SQL:查询购买了所有指定商品的人
表结构:
用户购买过的商品记录表product_buy主要列:
user_name product_id
A 1
B 1
C 2
A 3
A 2
查询同时购买了product_id为 1,2,3的SQL语句:
SELECT user_name
FROM product_buy
WHERE product_id in(1,2,3)
GROUP BY user_name
HAVING COUNT(DISTINCT product_id) = 3;