我有兩個表,orders
和ordered_products
。MYSQL - 選擇表A中的值,其中表B中的所有對應值都具有特定值
ORDERS
|ORDERS_ID|CUSTOMER NAME|...
|1 |PIPPO |...
|2 |PLUTO |...
ORDERED PRODUCTS
|ORDERED_ID|ORDERS_ID|PRODUCT |PRODUCT_TYPE|...
|1 |1 |ProdottoA| 1 |...
|2 |1 |ProdottoB| 2 |...
|3 |1 |ProdottoC| 1 |...
|4 |2 |ProdottoD| 2 |...
我需要兩個疑問,第一,選擇具有類型1的至少一個產品的所有訂單,第二個選擇具有類型1
的所有產品的所有訂單第一個我有以下查詢解決:
select distinct orders_id from ORDERS o left join ORDERED_PRODUCTS op on (o.orders_id=op.orders_id) where op.product_type = '1'
但我找不到第二個查詢的解決方案。
找到解決方案!我用:
select distinct orders_id from ORDERS o left join ORDERED_PRODUCTS op on (o.orders_id=op.orders_id)
where
(select count(ordered_id) from ordered_products op where op.orders_id = o.orders_id)
=
(select count(ordered_id) from ordered_products op where op.orders_id = o.orders_id and op.orders_products_categorizzazione='1')
感謝您的幫助