這裏是我的表的子集:優化SQL查詢取消訂單
orders:
- order_id
- customer_id
order_products:
- order_id
- order_product_id (unique key)
- canceled
我想選擇一個給定客戶的所有訂單(ORDER_ID)(CUSTOMER_ID),所有的產品訂單都是取消了,不僅僅是一些產品。是否有這樣做比這更優雅的或有效的方法:
select order_id from orders
where order_id in (
select order_id from orders
inner join order_products on orders.order_id = order_products.order_id
where order_products.customer_id = 1234 and order_products.canceled = 1
)
and order_id not in (
select order_id from orders
inner join order_products on orders.order_id = order_products.order_id
where order_products.customer_id = 1234 and order_products.canceled = 0
)
什麼數據庫引擎,您使用的,即MySQL和SQL服務器-2000,SQL服務器2005,Oracle等 – chadhoc 2009-11-20 16:31:20