我的问题如下。
SELECT A.ItemID, A.MSRP, A.SalesPrice, A.Cost, A.DiscountPercent,
B.QuantityOnHand, B.QuantityAvailable, C.OrderRate, C.OrderQuantity
FROM item_info A
LEFT JOIN inventory_status B
ON A.ItemID = B.ItemID
LEFT JOIN order_line C
ON A.ItemID = C.OrderLineItemID
WHERE A.Name LIKE ?
AND C.OrderRate != 0
AND C.OrderQuantity != 0
ORDER BY C.OrderRate ASC, C.OrderQuantity ASC
LIMIT 1
基本上,我们的想法是搜索一些项目并检索其基本信息、库存数量和历史数据。在修改此查询以获取历史数据之前,它工作得很好,只要在
null
表中没有条目的情况下为QuantityOnHand
和QuantityAvailable
都提供inventory_status
,而其其余信息将正常返回。但是,尝试加入到第三个表不再这样做。现在,什么都不还了。是否有任何方法来修改这一点,以便在“cccc>表中没有检索到的项的记录的情况下,得到OrrRead和Orror量的null
? 最佳答案
你必须移动谓词
C.OrderRate != 0 AND
C.OrderQuantity != 0
从
WHERE
到ON
子句:SELECT A.ItemID, A.MSRP, A.SalesPrice, A.Cost, A.DiscountPercent,
B.QuantityOnHand, B.QuantityAvailable, C.OrderRate, C.OrderQuantity
FROM item_info A
LEFT JOIN inventory_status B
ON A.ItemID = B.ItemID
LEFT JOIN order_line C
ON A.ItemID = C.OrderLineItemID AND
C.OrderRate != 0 AND
C.OrderQuantity != 0
WHERE A.Name LIKE ?
ORDER BY C.OrderRate ASC, C.OrderQuantity ASC
LIMIT 1
否则,
LEFT JOIN
操作变为INNER JOIN
。