简体   繁体   English

高级SQL选择查询/动态列

[英]advanced SQL select query / dynamic columns

I'm leaning MySQL but struggling with more advanced queries, so I hope someone could shed some light 我倾向于使用MySQL,但仍在尝试更高级的查询,因此希望有人可以提供一些帮助

I have the following database (I hope it's properly formated) 我有以下数据库(希望它的格式正确)

table_visitors
visitor_id | visitor_name
   1       |       Joe
   2       |       Bob

table_rooms
room_id | room_name
    1   |  room 1
    2   |  room 2 
    3   |  room 3
    4   |  room 4

table_roomsvisitors (indicates which visitors can access which rooms)
visitor_id | room_id
    1      |    1
    1      |    2
    1      |    3
    2      |    1
    2      |    4 

I'd like to list (for a specific visitor) every item in table_rooms as well if the selected visitor is allowed to access each room 如果要允许所选访问者访问每个房间,我还要列出(针对特定访问者)table_rooms中的每个项目

The expected result should be something like that: 预期的结果应该是这样的:

query for Joe:
room_name | access
 room 1   | true
 room 2   | true
 room 3   | true
 room 4   | false

query for Bob:
room_name | access
 room 1   | true
 room 2   | false
 room 3   | false
 room 4   | true

You could use a left join and case statement to check if user has access to room 您可以使用左连接和案例语句来检查用户是否有权访问会议室

select r.room_name, 
       case when rv.visitor_id is not null then true else false end access
from table_rooms r
left join table_roomsvisitors rv on r.room_id = rv.room_id
and rv.visitor_id = 1

demo 演示

left join will produce null where there is no association found for room in table_roomsvisitors based on additional join condition as rv.visitor_id = 1 which will join only rows from table_roomsvisitors where visitor_id is one and for other rows it will return null, thus in select part you could check for not null rows and return true, false for null rows table_roomsvisitors根据其他rv.visitor_id = 1条件(在rv.visitor_id = 1情况下,在table_roomsvisitors未找到房间的关联)产生null,它将仅table_roomsvisitors来自visitor_id为1的table_roomsvisitors中的行,对于其他行,它将返回null,因此在选择部分您可以检查不为空的行并返回true,为空的行返回false

You cam use CROSS JOIN : 您可以使用CROSS JOIN

select v.visitor_name, r.room_name, 
       (case when rv.visitor_id is null then 'false' else 'true' end) as access
from table_visitors v cross join 
     table_rooms r left join
     table_roomsvisitors  rv 
     on rv.visitor_id = v.visitor_id and rv.room_id = r.room_id
where v.visitor_name = 'Joe';

joined the tables and then used a case for true and false. 加入表,然后使用大小写为true和false。 Then the id is based on who since you said a query for bob and one for joe 那么id是基于谁的,因为您说的是bob和joe的查询

For Joe: 对于乔:

select tr.room_name, case when rv.visitor_id is not null then 'true' else 'false' end access from table_rooms tr
left join table_roomvisitors rv on tr.room_id = rv.room_id
where rv.visitor_id = 1

For Bob: 对于鲍勃:

select tr.room_name, case when rv.visitor_id is not null then 'true' else 'false' end access from table_rooms tr
left join table_roomvisitors rv on tr.room_id = rv.room_id
where rv.visitor_id = 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM