【发布时间】:2016-09-14 23:05:21
【问题描述】:
以下查询让我感到困惑。如果我执行WHERE (inventory_to_pos.POSID IS NULL OR inventory_to_pos.POSID = ?) 并绑定POSID,它可能不显示products 表中的所有行,而inventory_to_pos 中可能存在也可能不存在。当LEFT JOIN-ing 表时,我是否应该从原始表中获取 all 行,当我只过滤原始表并在 LEFT JOIN'ed 表的任何条件下使用 IS NULL 时?
SELECT products.ID,products.NAME,products.VOLUME,productcombinations.PRODUCTID,productcombinations.PART,inventory_to_pos.FULLCOUNT
FROM products
LEFT JOIN productcombinations ON products.ID = productcombinations.PARTOF
LEFT JOIN inventory_to_pos ON products.ID = inventory_to_pos.PRODUCT
WHERE products.INVENTORY = 1
AND products.AVAILABLE = 1
AND products.ID > 0
AND (inventory_to_pos.POSID IS NULL OR inventory_to_pos.POSID = ?);
如果给定的产品和 POSID 不存在 inventory_to_pos.PRODUCT 和 inventory_to_pos.POSID,则我不会得到任何行。为什么?
【问题讨论】:
-
LEFT JOIN + WHERE = INNER JOIN(在同一张表上),所以将这些附加条件放入 LEFT JOIN 语句中。
-
如何相等?我没有在左连接的列上使用 WHERE?你能举例说明你的意思吗?
-
你在尝试
WHERE inventory_to_pos.POSID IS NULL,对吧? -
是的,但是 OR 有值 X(绑定)?我看不出有什么不同。
-
请发布一个带有修复的答案,以便我理解。