LEFT JOIN 删除行而不过滤原始 table
LEFT JOIN drops rows without filtering original table
以下查询让我感到困惑。它 不 显示 products
table 中的所有行,如果我这样做 WHERE (inventory_to_pos.POSID IS NULL OR inventory_to_pos.POSID = ?)
并绑定可能存在或可能不存在的 POSID
在 inventory_to_pos
。当 LEFT JOIN
-ing a table 时,当我只过滤原始 [=29] 时,我是否应该从原始 table 中获取 all 行=] 并在 LEFT JOIN
的 tables?
的任何条件下使用 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 = ?);
如果 inventory_to_pos.PRODUCT
和 inventory_to_pos.POSID
对于给定的产品和 POSID 不存在,则我没有任何行。为什么?
将所有相关的 invetory_to_pos
子句移动到 LEFT JOIN 中,即:
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 AND (
inventory_to_pos.POSID IS NULL
OR inventory_to_pos.POSID = ?
)
WHERE
products.INVENTORY = 1
AND products.AVAILABLE = 1
AND products.ID > 0
以下查询让我感到困惑。它 不 显示 products
table 中的所有行,如果我这样做 WHERE (inventory_to_pos.POSID IS NULL OR inventory_to_pos.POSID = ?)
并绑定可能存在或可能不存在的 POSID
在 inventory_to_pos
。当 LEFT JOIN
-ing a table 时,当我只过滤原始 [=29] 时,我是否应该从原始 table 中获取 all 行=] 并在 LEFT JOIN
的 tables?
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 = ?);
如果 inventory_to_pos.PRODUCT
和 inventory_to_pos.POSID
对于给定的产品和 POSID 不存在,则我没有任何行。为什么?
将所有相关的 invetory_to_pos
子句移动到 LEFT JOIN 中,即:
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 AND (
inventory_to_pos.POSID IS NULL
OR inventory_to_pos.POSID = ?
)
WHERE
products.INVENTORY = 1
AND products.AVAILABLE = 1
AND products.ID > 0