我很难找到这个问题。我知道这给了我供应商的所有供应商名称,供应商提供项目中存在的所有零件。但只是因为我在网上找到了答案!
select sname
from s
where not exists (select *
from p
where not exists (select *
from spj spjx
where s.sno = spjx.sno and
p.pno = spjx.pno
)
);
答案 0 :(得分:4)
有助于重新格式化:
select sname from s -- show all supplier names
where not exists -- that there is not
(select * from p -- a part
where not exists -- that is not
(select * from spj spjx -- supplied
where s.sno = spjx.sno -- by them
and p.pno = spjx.pno));
基本上:从s中选择所有sname,其中没有p存在于没有spj的地方,spj匹配s和p。将每个层视为过滤器。
结果看起来像是一个关系师,正如马丁在评论中指出的那样。
答案 1 :(得分:1)
您可以将其视为集过滤集。这里有三组:
select * from spj spjx
where s.sno = spjx.sno and
p.pno = spjx.pno
select * from p
where not exists ({previous set})
select sname from s
where not exists ({previous set})
因此,在您看到{previous set}
的任何地方,外部集合都会被该集合的结果过滤。
此外,为了完整性,当你看到这个:
from spj spjx
相当于:
from spj AS spjx
因此在此示例中将spjx
设为alias
。
答案 2 :(得分:0)