Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 左联接在不过滤原始表的情况下删除行_Mysql_Left Join - Fatal编程技术网

Mysql 左联接在不过滤原始表的情况下删除行

Mysql 左联接在不过滤原始表的情况下删除行,mysql,left-join,Mysql,Left Join,下面的问题让我感到困惑。如果我在位置(inventory\u to_pos.POSID为NULL或inventory\u to_pos.POSID=?)显示并绑定POSID中可能存在或可能不存在的POSID,则它不会显示products表中的所有行。当LEFT JOIN-ing一个表时,如果我只对原始表进行筛选,并且在LEFT JOIN的表的任何条件下使用IS NULL,我是否应该从原始表中获取所有行 SELECT products.ID,products.NAME,products.VOLUM

下面的问题让我感到困惑。如果我在
位置(inventory\u to_pos.POSID为NULL或inventory\u to_pos.POSID=?)显示
并绑定
POSID
中可能存在或可能不存在的
POSID,则它不会显示
products
表中的所有行。当
LEFT JOIN
-ing一个表时,如果我只对原始表进行筛选,并且在
LEFT JOIN
的表的任何条件下使用
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\u to_pos.PRODUCT
inventory\u to_pos.POSID
,则不会给我任何行。为什么?

将所有相关的
invetory\u to\u pos
子句移到左连接中,即:

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

LEFT JOIN+WHERE=内部联接(在同一个表上),所以将这些附加条件放入LEFT JOIN语句中。这如何相等?我没有在左侧连接的柱上使用WHERE?你能举个例子说明你的意思吗?你正在尝试
库存到位置POSID为空的地方,对吗?是的,但是有值X(绑定)?我看不出有什么不同。请发布一个带有修正的答案,这样我才能理解。它起作用了。哇!这是一个巨大的差距,在我的一般知识如何过滤连接。你也许可以解释给我为什么我们不能过滤在哪里?我一直认为这是所有过滤器应该去的地方,一般来说,因为MySQL首先过滤表本身(它接受所有where语句),然后进行连接。因此,在您的情况下,引擎首先将根据WHERE子句过滤
库存到位置
,然后将其加入,这等于内部加入。但是。。。。根据WHERE子句,我希望行为NULL或具有特定值。我无法理解这如何等同于内部联接,它需要两个表中的行?完全被难住了。如果您执行
操作,其中inventory\u to_pos.POSID为空
插入左联接,引擎将只从
产品
表中获取与此条件匹配的相应记录,以及
产品.ID=inventory\u to_pos.PRODUCT
。使用左联接,引擎首先获取所有
产品
行,然后联接第二个表并将其行“粘合”到原始表。希望您理解这些解释的区别:)