[SQL]不知道1

表结构,companyid是公司名,productid是产品号,说明每个公司生产多少种产品。

companyid   productid
A
A
B
B
B
C
D
D
D 要求:取出所有生产的产品包含公司A所生产产品的所有公司名。
例如,公司A生产1,,那么产品中至少包含1,(可以更多)的公司名被选出,即A,B,D 求一句实现的sql语句。

方案一

select  a.companyid
from 你的表 a
where exists (select from 你的表 b where b.companyid='A' and a.productid =b.productid )
group by a.companyid
having count(a.productid)>=

方案二

select companyid from T A
where not exists
(select from T where companyid='A' and productid not in
(select productid from T where companyid = A.companyid))
上一篇:[Python]peewee 使用经验


下一篇:swiper笔记