我得到了这张表:http://www.dofactory.com/sql/sandbox我需要找到过去三个月没有订购的产品。 到目前为止,我得到了这个,我不知道我是如何实际展示那些没有订购的产品。需要一些帮助!谢谢!
SELECT ProductName
FROM Product
INNER JOIN OrderItem ON Product.Id=OrderItem.ProductId
INNER JOIN [Order] ON [Order].Id=OrderItem.OrderId
WHERE OrderDate BETWEEN '2014/01/30' AND '2014/04/30'
GROUP BY ProductName
答案 0 :(得分:1)
这样的查询建议not exists
。但是,left join
也有效:
SELECT p.ProductName
FROM Product p LEFT JOIN
OrderItem oi
ON p.Id = oi.ProductId LEFT JOIN
[Order] o
ON o.Id = oi.OrderId AND
o.OrderDate BETWEEN '2014-01-30' AND '2014-04-30'
WHERE o.Id IS NULL