[英]Select rows with specific multiple values from the same column?
我有两个角色不能相互关联的数据库,我需要显示角色冲突的所有用户。
例如:(id 2)会计师也不能同时是(id 5)培训师
这必须在不使用CTE的情况下完成
Table a Table b table c
--------------- ------------------- ------------
userID | roleID roleID | conflictID roleID | Role Name
1 2 2 5 1 chef
1 3 2 accountant
1 5 3 driver
2 3 4 barmaid
2 1 5 trainer
3 2
3 3
结果应仅包含同时具有角色2和5的userID
userID
------
1
将b表与a表连接两次,以获取具有冲突组合的userID:
select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
and a1.userID = a2.userID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.