SQL部门在mysql中使用'not exists'

2 mysql sql

我有下表:


'committee' table

commname    profname
========================
commA       bill
commA       jack
commA       piper
commB       bill
commB       piper

而且我正在努力寻找那些'吹笛者'所在的每个委员会的教授(答案应该是吹笛手和账单):

我有以下SQL分区查询,但它是错的,我无法弄清楚问题出在哪里(不返回账单,只是piper):


select b.profname
from committee b
where not exists 

(select commname
from committee a
where profname = 'piper' and not exists 

(select commname
from committee
where a.profname=b.profname ))

有人可以帮我这个吗?谢谢,

yst*_*sth 8

你的内心选择在where子句中没有使用任何东西,所以它总是为piper找到一些东西.尝试

select distinct b.profname from committee b
where not exists (
    select commname from committee a
    where a.profname = 'piper' and not exists  (
        select commname from committee c
        where c.profname=b.profname and c.commname=a.commname
    )
);
Run Code Online (Sandbox Code Playgroud)