我有2张桌子
Product
------------------------------
productID | productName
------------------------------
1 | Banana
2 | Apple
3 | Pie
4 | Dragon Fruit
5 | Watermelon
6 | Kiwi
7 | Nugget
Purchase
-------------------------------------
purchaseID | productID | purchaserID
-------------------------------------
1 |1 |1
2 |2 |1
3 |3 |1
4 |4 |2
5 |5 |2
6 |2 |4
7 |1 |5
如何从购买表中购买购买产品ID 1和2和3的购买者ID(购买者ID 1)? TYVM!
答案 0 :(得分:1)
这是" set-within-sets"查询。灵活的方法是使用group by
和having
:
select purchaserId
from purchases
where productid in (1, 2, 3)
group by purchaserId
having count(distinct productid) = 3;