Put value in custom defined column in sql query - mysql

I would like to create a sql query which create a column that doesn't exists in the db tables and gets fills on whether a row exists or not exists in a specific table.
For example:
I have 3 tables:
Users (For users list) - UID, UName
Locations (List of all available locations) - LID, LName
UsersLocations (All the locations the users have checked into) - UserID, LocationID
I need a sql query that from a user id get me a table of all the locations with a column that says whether the user has been in this location or not.
Example for Users table
UID | UName
1 John
4 Amy
5 Dann
Example for Locations table:
LID | LName
1 London
2 Barcelona
3 Paris
4 New York
Example for UsersLocations table:
UserID | LocationID
5 1
5 2
Example for output (for userid = 5):
User ID | Location | Was Here
5 London true
5 Barcelona true
5 Paris false
5 New York false
The output needs to include all the Locations from the locations table.
Also the UsersLocations table only contains the userID of the users that checked into that location.

Hmmm. One method is a correlated subquery:
select u.userid, l.location,
(case when exists (select 1 from userLocations where ul.userid = u.userid and ul.lid = l.locationid)
then 'true'
else 'false'
end) as WasHere
from location l cross join
(select 5 as userid) u;
The only part that is database-specific is the subquery for u.

Related

How to find the dependency that brought another dependency in MySQL or Redshift

There is two tables:
dependency_permission table:
id
dependency_permission_id
2
1
4
2
user_permission table:
id
user_id
permission_id
1
11111
1
2
22222
4
3
22222
2
4
11111
2
5
33333
2
I want to write a query that finds all user_id's which have permissions depend on another permission the user doesn't have.
from the above data, users (22222 and 33333) should be returned, they don't have permission 1 which 2 depends on.
You may left join the 'user_permission' table with the 'dependency_permission' to get the 'dependency_permission_id' for each user 'permission_id', then use NOT EXISTS operator to check the existence of 'dependency_permission_id' for each user 'permission_id'.
with user_dependency_permission as
(
select U.user_id, U.permission_id, D.dependency_permission_id
from user_permission U left join dependency_permission D
on U.permission_id = D.id
)
select user_id, permission_id /* if you want to select only user_ids use distinct user_id*/
from user_dependency_permission T
where not exists(
select 1 from user_dependency_permission D
where T.user_id=D.user_id and
D.permission_id=T.dependency_permission_id
)
and T.dependency_permission_id is not null
See a demo on MySQL.

Query two tables where one of them has been transposed

I have the following tables, USER and RECORDS. The USER table simply has the name of the user and the group they have access to. If a user has null as their group (such as BOB does), that means they are a super user and have access to all the groups.
USER:
username | group
---------|--------
Bob | (null)
Kevin | 1
John | 2
Mary | 1
I then have a RECORDS table that has a list of records:
record | field_name | value
----------------------------
1 AGE 92
1 HEIGHT 9'2
1 __GROUP__ 1
2 AGE 11
2 HEIGHT 1'1
2 __GROUP__ 2
3 AGE 68
3 HEIGHT 6'8
This is not my design but it is what I have to query on. In the RECORDS table, the group that a record belongs to is indicated by the __GROUP__ value in the field_name column. So what I am trying to do is get a list of all the records each user has access to.
So based on the user table:
BOB has access to all the records
KEVIN and MARY only have access to records who have a field_name =
'__ GROUP __' and value = 1
JOHN only have access to records who have a field_name = '__ GROUP __'
and value = 2
This means
BOB has access to records 1, 2, and 3.
Kevin has access to only record 1
Mary has access to only record 1
John only has access to record 2
If the GROUP is missing from a record that means only a super user ( a user with group = null) can access it.
I understand if in the RECORDS table it would have a "GROUP" column this would be easier, but unfortunately, this isn't my database and I cannot make changes to the structure.
**Sorry, I forgot to mention it is possible for a user to be in multiple groups! For instance, Mary could be in groups 1 and 2.
One option uses exists:
select u.*, r.*
from user u
cross join records r
where u.grp is null or exists (
select 1
from records r1
where
r1.ecord = r.record
and r1.field_name = '__GROUP__'
and r1.value = u.grp
)
This gives you the entire records rows that each user has access to.
If you just want the list of record ids, you can use aggregation instead:
select u.userame, r.record
from users u
cross join records r
group by u.userame, u.grp, r.record
having
u.grp is null
or max(r.field_name = '__GROUP__' and r.value = u.grp) = 1

Three table with value join query base value is first ,secound and third table

i need to develop the query with join or any thing i can't make it
my concept is the first table have a usr id and usr type id
the second table have details of usr type and id and usr master details id
the third table have usr master details and id my question is how i select the value of those three table
Sample table is
user_details
usr_id Name us_ty_id
25 john 2
34 sam 3
24 rose 1
user_type
us_ty_id type usr_ma_id
1 dev 2
2 desi 1
3 test 2
user_master
usr_ma_id details
1 team1
2 team2
3 team3
my output like below
usr_id type details
34 test team3
the first table us_type_id find the type and usr_ma_id in second and find the details of selected id of usr_ma_id in second table and find the details
ple help me...
What about the simple:
SELECT *
FROM user_details d
INNER JOIN user_type t ON t.us_ty_id = d.us_ty_id
INNER JOIN user_master m ON m.usr_ma_id = t.usr_ma_id;

mysql select matching results

I'm trying to find the leagues (lid) where two users are apart of.
Here are my tables:
Table leagues:
*id* lname
--------------
1 Hard C
3 Fun
5 Crazy
Table match:
*userid* *lid*
-----------------
1 1
4 5
1 3
2 1
4 1
4 3
*Are primary keys
match.lid is foreign key to leagues.id (a user cannot not be part of the same league twice)
Here's what I have so far (a start):
SELECT t1.lid, t2.lname
FROM match t1
JOIN leagues t2 on t1.lid = t2.id
So far I managed to join the two tables and get the names. My ultimate goal is to show the lid's where two users are part of the same league, say userid 1 and 4.
userid 1 is a member of lid 1 and 3
userid 4 is a member of lid 5, 1, and 3
Both users meet in league(lid) 1 and 3
So I need a query that shows only the league where both users meet. Like this:
lid lname
--------------
1 Hard C
3 Fun
Since userid 1 and 4 meet in league 1 and 3, the results should show that. I can run two queries for each user and check which leagues both users meet via php, but I think it's more efficient to run one query.
SELECT m1.lid, l.lname FROM
`match` m1, `match` m2, leagues l
WHERE m1.lid = m2.lid AND m1.lid = l.id
AND m1.userid = 1
AND m2.userid = 4
There are a few ways. The most straightforward is:
SELECT id AS lid,
lname
FROM leagues
WHERE id IN
( SELECT lid
FROM match
WHERE userid = 1
)
AND id IN
( SELECT lid
FROM match
WHERE userid = 4
)
;
Another way, which is a bit less direct, but may perform better — you can try it and see — is to use JOIN:
SELECT id AS lid,
lname
FROM leagues
JOIN match AS match1
ON match1.lid = leagues.id
AND match1.userid = 1
JOIN match AS match2
ON match2.lid = leagues.id
AND match2.userid = 4
;

Count Memberships of User in Groups

I've a tree structure of an association which is divided in divisions, subdivisions etc. on every level users may have memberships to certain roles.
I want to count the memberships on every "structure type" (association, division, subdivision) as defined in the table
The Table structure looks like:
table intern_structures
Contains the hierarchy (nested set, but that does not matter here)
id | intern_structure_type_id | name | parent_id | lft | rgt
1 1 My Company USA 0 1 6
2 2 Texas 1 2 5
3 3 El Paso 2 3 4
table intern_structure_types
Contains Description to the types like "association", "division", "subdivision"
id | name
1 Association
2 Division
3 Subdivision
table memberships
Contains the memberships
id | user_id | intern_structure_id | role_id
1 1 1 1
2 1 2 2
3 2 3 1
3 2 3 3
....
table roles
Contains role descriptions
id | name
1 Admin
2 Moderator
3 Clerk
I want a grouped list like:
structure_type_name | role_name | count of memberships
Association Admin 1
Association Moderator 10
Association Clerk 0 << !! I miss the zero rows!
Division Admin 7
Divison Moderator 43
Division Clerk 31
Subdivision Admin 234
Subdivision Moderator 942
Subdivision Clerk 456
What I achieved so far is this query:
SELECT
is_types.name,
roles.name,
COUNT(memberships.id)
FROM
roles,
intern_structure_types AS is_types
LEFT JOIN intern_structures AS is_elements ON is_elements.intern_structure_type_id = is_types.id
LEFT JOIN memberships ON memberships.intern_structure_id = is_elements.id
WHERE
roles.id = memberships.role_id
GROUP BY
is_types.id, roles.id
It works fine except that it doesn't list all roles because some roles don't have any memberships yet but I want them listed as well just with 0 as membership count.
I'd be very thankful for any help!
I'm assuming the counts you showed in the OP are contrived. To get the results you want, you should create a derived table of the types and roles in use and then left join that entire query to a cross join of the roles and types.
Select is_types.name
, roles.name
, Count(Z.is_type_name)
From roles
Cross Join intern_structure_types As is_types
Left Join (
Select is_types.name As is_type_name
, roles.name As role_name
From intern_structures As is_elements
Join intern_structure_types As is_types
On is_types.id = is_elements.intern_structure_type_id
Join memberships
On memberships.intern_structure_id = is_elements.id
Join roles
On roles.id = memberships.role_id
) As Z
On Z.is_type_name = is_types.name
And Z.role_name = roles.name
Group By is_types.name, roles.name