零件表
pid ____ 10 20 30
供应商表
sid ____ 1 2 3
目录日志表
cid|pid|sid ___________ 1 | 10 | 1 2 | 10 | 2 3 | 10 | 3 4 | 20 | 1 5 | 20 | 2
我希望获得所有供应商生产的零件
我想要该样品的结果10 ...
我写了这个查询(demo)
select p.pid,count(c.pid) from p join c on(c.pid=p.pid)
group by c.pid
having count(c.pid) = (select count(1) from s)
问题:有更好的方法来获取所有供应商生产的零件。
答案 0 :(得分:1)
一项改进可能是处理c.sid
个重复项:
having count(distinct c.sid) = (select count(1) from s)
答案 1 :(得分:1)
如果您只需要产品ID(pid),则不必加入。您可以仅在目录表上执行此操作,例如:
SELECT pid
FROM c
GROUP BY pid
HAVING count(DISTINCT sid) = (SELECT count(DISTINCT sid) FROM s)