我的问题如下。

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表中没有条目的情况下为QuantityOnHandQuantityAvailable都提供inventory_status,而其其余信息将正常返回。但是,尝试加入到第三个表不再这样做。现在,什么都不还了。是否有任何方法来修改这一点,以便在“cccc>表中没有检索到的项的记录的情况下,得到OrrRead和Orror量的null

最佳答案

你必须移动谓词

C.OrderRate != 0 AND
C.OrderQuantity != 0

WHEREON子句:
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

07-24 09:37
查看更多