[英]SQL query with LEFT JOIN returning no results
I have 2 MySQL tables IM_Transfer
and IM_INV
. 我有2个MySQL表IM_Transfer
和IM_INV
。 I run the following query regularly: 我定期运行以下查询:
SELECT
`IM_Transfer`.`sku`,
`IM_Transfer`.`vendor_sku`,
`IM_Transfer`.`Description`,
`IM_Transfer`.`Receiver`,
`IM_INV`.`LOC_ID`,
`IM_INV`.`QTY_ON_HND`
FROM
`IM_Transfer`
LEFT JOIN
`IM_INV`
ON
`IM_Transfer`.`sku` = `IM_INV`.`ITEM_NO`
WHERE `LOC_ID` = "AF"
OR `LOC_ID` = "LO"
OR `LOC_ID` = "S"
OR `LOC_ID` = "SL"
ORDER BY
`IM_Transfer`.`sku`,
`IM_INV`.`LOC_ID`
ASC
Most of the time, most of the lines in IM_Transfer
match by IM_Transfer
. 大多数时候,大多数的行IM_Transfer
通过比赛IM_Transfer
。 sku
and IM_INV
. sku
和IM_INV
。 ITEM_NO
and the ones that don't come back with null
values in the last 2 columns. ITEM_NO
和最后两列中没有返回null
值的项目。
I just ran this query with aa single row in IM_Transfer
and it does not have any matching data in IM_INV
. 我只是跑这个查询与AA单列IM_Transfer
和它没有任何匹配的数据IM_INV
。 How do I update the query to still return a row with just null
in the LOC_ID
and QTY_ON_HND
columns? 如何更新查询以仍然在LOC_ID
和QTY_ON_HND
列中返回仅包含null
的行?
Thanks in advance 提前致谢
The where clause only evaluates to true when there is a row in the outer joined table. 仅当外部联接表中有一行时,where子句的计算结果才为true。 Referencing a column from the outer joined table in a where clause usually makes the join into an inner join. 在where子句中引用外部联接表中的列通常会使联接成为内部联接。
I think that the query that you want is: 我认为您想要的查询是:
SELECT
`IM_Transfer`.`sku`,
`IM_Transfer`.`vendor_sku`,
`IM_Transfer`.`Description`,
`IM_Transfer`.`Receiver`,
`IM_INV`.`LOC_ID`,
`IM_INV`.`QTY_ON_HND`
FROM
`IM_Transfer`
LEFT JOIN
`IM_INV`
ON
`IM_Transfer`.`sku` = `IM_INV`.`ITEM_NO`
AND ( `LOC_ID` = "AF"
OR `LOC_ID` = "LO"
OR `LOC_ID` = "S"
OR `LOC_ID` = "SL" )
ORDER BY
`IM_Transfer`.`sku`,
`IM_INV`.`LOC_ID`
ASC
Notes: 笔记:
LOC_ID IN ("AF","LO","S","SL")
用LOC_ID IN ("AF","LO","S","SL")
表示时,您的LOC_ID过滤器更简单 Thanks @SeanLange for pointing me in the right direction. 感谢@SeanLange为我指出正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.