如何获得所有未回答的订单

时间:2014-12-05 13:14:41

标签: sql sqlite

有以下数据库方案:

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 个答案:

答案 0 :(得分:1)

您可以使用not innot 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;
相关问题