MySQL查找没有现有行的ID

时间:2016-10-18 09:29:23

标签: mysql sql

我有三张桌子:

server1

在带有查询的MySQL中是否可以获取PersonRoles表中没有具有确切角色的行的人员ID。例如,如果我想检查" Designer"查询应返回的角色:2和3

2 个答案:

答案 0 :(得分:1)

以下是您的解决方案:

select person_id from Person
                where person_id not in 
               (select person_id from Role r 
                   inner join PersonRoles pr on pr.role_id=r.role_id
                where r.description='Designer')

答案 1 :(得分:0)

您可以使用not exists运算符:

SELECT *
FROM   Person p
WHERE  NOT EXISTS (SELECT *
                   FROM   Role r
                   JOIN   PersonRoles pr ON r.role_id = pr.role_id
                   WHERE  r.description = 'Designer' AND
                          pr.person_id = p.person_id)