我有商店表,产品表和商店-产品表,显示了它们之间的关系。
*store table*
store_id name ...
1 store1
2 store2
3 store3
*product table*
product_id name ...
1 product1
2 product2
3 product3
*store-product table*
id store_id product_id
1 1 1
2 1 2
3 2 3
4 3 1
5 3 2
6 3 3
提供产品后,我想找到销售这些产品的商店。
即:如果给定的产品是1
,1, 3
则应获取商店。
如果给定的产品为1, 2, 3
,则仅3
应获取商店。
您可以使用group by
和having
:
select sp.store_id
from store_product sp
where sp.product_id in (1, 2, 3) -- list of products
group by sp.store_id
having count(*) = 3; -- number of elements in list
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句