题目难度
中等
题目描述
写一条 SQL 查询语句,从 Customer 表中查询购买了 Product 表中所有产品的客户的 id。
正确答案
select customer_id
from Customer
group by customer_id
having count(distinct product_key)=(select count(distinct product_key) from Product)
思想
所有的商品都在商品表中,只要一个人购买的不同种商品数量与商品表中不同商品数量相同,即为购买全部的商品。
- 易错点:商品表中可能出现重复商品,消费者也可能重复购买商品,所以需要对这两方面都去重。
题目来源
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/customers-who-bought-all-products