我有两张桌子,orders
和ordered_products
。
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')
感谢您的帮助