2014-12-05 39 views
0

有以下分貝方案:如何獲得所有未回答的訂單

 
Products: 

ID|ProductType 
--|----------- 
1 |Car 
2 |PC 

Orders 

ID|CustomerID|PruductID 
--|-------------------- 
1 |  2 | 1 
2 | 12 | 2 
3 | 12 | 1 

Bill 
ID|OrderID|Price 
--|-------|----- 
1 | 2 | 200 
2 | 3 | 2000 

如何查詢哪些沒有一個法案還和獲取數據的訂單數據庫:

 
CustomerID|PruductID|ProductType 
----------|---------|----------- 
    2 | 1 | Car 

感謝

回答

1

您可以使用not in,not existsleft join。這裏是後者:

select o.CustomerId, o.ProductId, p.ProductType 
from orders o join 
    products p 
    on o.productId = p.Id left join 
    bills b 
    on b.orderId = o.Id 
where b.id is null; 
+0

btw。謝謝Gordon – Amios 2015-07-22 11:17:45