从表中返回与另一个表的所有记录匹配的记录

时间:2013-05-14 15:31:28

标签: sql join

在DBMS中包含以下三个表:

Customer(Id, Name, City),
Product(Id, Name, Price),
Orders(Cust_Id, Prod_Id, Date)

获取已订购所有产品的客户(如果有)的查询是什么?

1 个答案:

答案 0 :(得分:3)

select c.id
from customer c
inner join orders o on o.cust_id = c.id
inner join product p on p.id = o.prod_id
group by c.id
having count(distinct p.id) = (select count(id) from product)