从同一列中选择具有特定多个值的行?

问题描述 投票:0回答:1

我有数据库,其中2个角色不能相互关联,我需要显示任何具有冲突角色的用户。

例如:(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
sql sqlite
1个回答
1
投票

使用a表两次加入b表,以获得具有冲突组合的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
© www.soinside.com 2019 - 2024. All rights reserved.