I have two tables tbl_role_master and tbl_user_master.
tbl_role_master has the below values
role_id role rolevalue
1 admin 1
2 gen 2
3 test1 4
4 test2 8
5 test3 16
rolevalue contains fibonacciseries..
tbl_user_master contains the below values
user_id user_name rolevlue_consolidated
1 user1 15
2 user2 3
3 user3 7
rolevlue_consolidated contains the addition of combination of rolevalue in tbl_role_master table
for ex: user1's 15 has the roles of admin,gen,test1 and test2
now i want a linq query to get the roles from tbl_role_master based on user_id in tbl_user_master
for ex if i passs user_id 2 the select query should return
admin
gen
i have sql query -
select
role
from
tbl_rol_master
WHERE (
select
rolevalue_consolidated
from
tbl_user_master
where
User_Id=" + userId + "
)
Assuming you have two lists, one with all the roles and one with all your users you can do something like this:
var userRoles = roles.Where(role => (role.RoleValue &
users.First(user => user.Id == YourUserId) == role.RoleValue);
Which returns you a list with all assigned rights of the user with the given user id.
Related
I have 2 tables in MySQL like this
Table DEPARTMENT
Id
Name
1
Department 1
2
Department 2
Table STAFF
Id
Department_Id
Name
1
1
Staff 1
2
1
Staff 2
3
2
Staff 3
4
1
Staff 4
STAFF table has about 10 million records.
All STAFF's informations has been pushed by Logstash to ElasticSearch. Each document in ElasticSearch now only have 3 fields are Staff_Id, Staff_Name and Department_Name. Something like this:
{
"Staff_Id": 1,
"Staff_Name": "Staff 1",
"Department_Name": "Department 1"
}
Because of practical needs, I need to add one more field called Department_Id to each document. Note that this field (Department_Id) does not exist on existing documents.
I am a newbie to both Logstash and ElasticSearch. How can I do this with Logstash? Interpreted in the SQL way would be:
SELECT * FROM DEPARTMENT;
UPDATE STAFF SET Department_Id = XXX WHERE Department_Name = YYY
Note that DEPARTMENT table has about 100.000 records and ElasticSearch has about 10 million documents.
Can you take a look?
I have a table users representing a hierarchical tree like this:
Column
Type
Comment
user_id
integer
sequence
user_type
integer
1 for group of users 2 for normal user
group_id
integer
Reference to a user in the same table with user_type = 1
user_name
varchar(xxx)
The group_id column references another user_id so that groups and users are stored in the same table.
The master group_id is 0.
Like this:
user_id
user_type
group_id
user_name
0
1
null
'All users'
5
2
0
'USER1'
6
2
0
'USER2'
11
1
0
'SUBGROUP1'
12
1
11
'SUBGROUP2'
13
2
12
'USER3'
20
1
0
'SUBGROUP3'
21
2
20
'USER4'
Notice that:
There can be gaps in user_id.
A group can contain nothing or any number of groups or users.
I have already managed to retrieve the full tree, properly indented and sorted, by using the connect by oracle statement.
This is not my question here.
My question is:
Given a user_id to a query, how to browse the tree up to the master group 'All Users'
and output as a result the full path from the leaf to the master group ?
Example 1: I run the query for USER1, i want the following output:
All Users
- USER1
Example 2: I run the same query for USER3, i want the following output:
All Users
- SUBGROUP1
-- SUBGROUP2
--- USER3
I hope someone could help me on this.
For information i post the query to retrieve the full tree, for you to see the use of connect by and start with.
I'm sure this query is close to the one i want, but my tries never produce the result i want.
select
lpad('-', (level - 1) * 2, ' ') || u.user_name as padded_name,
u.userid,
u.user_group,
u.user_type,
level
from users u
connect by prior u.user_id = u.group_id
start with u.user_id = 0
order siblings by upper(u.user_name);
You could use connect by to walk in the opposite direction. Then the level will of course be opposite too. So to get the results in the right order and indentation, chain another query based on these results that will use row_number() to determine the indentation:
with base as (
select
u.user_name,
u.user_id,
u.group_id,
u.user_type,
level as lvl
from users u
connect by prior u.group_id = u.user_id
start with u.user_id = 13
)
select
lpad('-', (row_number() over (order by lvl desc) - 1) * 2, ' ') || base.user_name
as padded_name,
user_id,
group_id,
user_type
from base
order by lvl desc;
I have 5 tables retailers, doctors, softwareproviders, members and contactpersons. contactpersons table associated with other 4 tables using type and refid.
retailers
id
name
contactno
mem_type
2
karthik
855550
1
members
id
name
contactno
6
naveen
855550
contactpersons
id
type
refid
name
1
1
2
ruban
1
2
6
aron
type 1 - retailers, 2 - members, 3 - doctors, 4 - softwareproviders
In contactpersons model how to make hasone relationship with other tables
public function retailersinfo(){
return $this->hasOne('App\Retailers','id','refid');
}
we need to add type condition (where condition =1).
I have one table scores where I have saving users scores. It's looks like this
table `scores`
id | points | user_id
1 5 1
2 2 1
3 4 1
4 1 3
5 10 2
I want to select each user, sum his points and show as a ranking. The result from above should be
user_id | points
1 11
2 10
3 1
The query with which I came up is
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->first();
The problem is in ->first() because it's return only one result.. it is working as must. If I try to use ->get() instead I've got Undefined property error. How should I use this?
The query which is working in phpmyadmin
SELECT count(id) as numberId, sum(points) as numberOfPoints FROM `points` GROUP BY `user_id`
You can use something like this
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->get();
foreach($sumPoints as $point){
dd($point); //OR dd($point->numberOfPoints)
}
I have a table containing a list of customers, a second table containing orders placed by my customers and a third table containing the line items for the orders.
I would like to be able to use a Linq query to get the customer name, the number of orders and the total value of all the orders placed by this customer.
Assuming the following data:
[Customers]
CustomerId Name
---------------------
1 Bob Smith
2 Jane Doe
[Orders]
OrderId CustomerId
---------------------
1 1
2 1
3 2
[OrderLineItems]
LineItemId OrderId UnitPrice Quantity
--------------------------------------------
1 1 5 2
2 1 2 3
3 2 10 10
4 2 4 2
5 3 2 5
I would like the following result:
Name OrdersCount TotalValue
--------------------------------------------
Bob Smith 2 124
Jane Doe 1 10
What would be the Linq query to get this result?
If you presume that you have a strongly typed datacontext and a partial class Customer that you are free to add to, I would solve this problem like this:
public partial class Customer {
public int NumberOfOrders {
get { return Orders.Count(); }
}
public int TotalValue {
get { return Orders.OrderLineItems.Sum( o => o.UnitPrice * o.Quantity); }
}
}
YourDataContext db = new YourDataContext();
var query = from c in db.Customers
select new {c.Name, c.NumberOfOrders,
c.TotalValue}
This would project a new anonymous type with the data you requested.