为此很难创建一个好标题。
给出表products
productID
---------
892
583
388
表purchases
customerID productID
---------- ---------
56 892
97 388
56 583
56 388
97 583
我该如何获取所有购买了所有产品的顾客的桌子?
答案 0 :(得分:2)
您可以使用group by
和having
:
select customerId
from purchases
group by customerId
having count(distinct productID) = (select count(*) from products);
答案 1 :(得分:0)
将GROUP BY
子句与HAVING
一起使用:
SELECT pr.customerID
FROM products p INNER JOIN
purchases pr
on pr.productID = p.productID
GROUP BY pr.customerID
HAVING COUNT(DISTINCT pr.productID) = (SELECT COUNT(*) FROM products);