SQL - 左连接到两个表不返回任何内容

时间:2017-01-04 16:49:58

标签: mysql

我的查询如下。

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

基本上,我们的想法是搜索某个项目并检索其基本信息,库存数量和历史数据。在修改此查询以获取历史数据之前,如果nullQuantityOnHand中的QuantityAvailableinventory_status都没有null,那么它就能完美地工作order_line 1}}表,而其余的信息将正常返回。但是,尝试加入第三个表不再这样做。现在,没有任何回报。有没有办法修改这个,以便在<script src=".../Scripts/jquery.validate.js"></script> <script src=".../Scripts/jquery.validate.unobtrusive.js"></script> 表中没有检索到项目的记录的情况下获得OrderRate和OrderQuantity的 def run_query connection.exec_query( "SELECT * FROM TABLE(FN_REQ(#{demo_type_param},#{demo_tid_param}}))") end

2 个答案:

答案 0 :(得分:4)

你必须移动谓词

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

答案 1 :(得分:0)

更改此部分

LEFT JOIN order_line C
ON A.ItemID = C.OrderLineItemID
WHERE A.Name LIKE ?
AND C.OrderRate != 0
AND C.OrderQuantity != 0

到这个

LEFT JOIN order_line C
ON A.ItemID = C.OrderLineItemID
AND C.OrderRate != 0
AND C.OrderQuantity != 0
WHERE A.Name LIKE ?