[英]Left Join and condition on Mysql
I want such a query that returns all rows from Table 1 jointest1 and the row that matches join condition with Table 2 jointest2 except the duplicate rows in jointest2 我想要这样一个查询,该查询返回表1 jointest1中的所有行以及与表2 jointest2中的连接条件匹配的行,但joinest2中的重复行除外
Table after join should look 连接后的表应该看起来
the query will be like 查询将像
SELECT * FROM jointest1 LEFT JOIN jointest2 ON jointest1.id=jointest2.j1_id WHERE jointest2.id NOT IN ( 2 )
but when I am adding WHERE jointest2.id NOT IN ( 2 )
Left join is not working , it is only returning row that is jointest1.id=jointest2.j1_id and NOT IN ( 2 )
但是当我添加WHERE jointest2.id NOT IN ( 2 )
左WHERE jointest2.id NOT IN ( 2 )
不起作用时,它只是返回的行是jointest1.id=jointest2.j1_id and NOT IN ( 2 )
Thanks in advance for help 预先感谢您的帮助
The left join
is working fine. left join
工作正常。 When you have a condition on the second table, it needs to go in the on
clause: 当您在第二张表上有条件时,它需要on
子句中:
SELECT *
FROM jointest1 LEFT JOIN
jointest2
ON jointest1.id = jointest2.j1_id AND
jointest2.id NOT IN ( 2 );
Otherwise, the condition (even NOT IN
) will return NULL
, turning the LEFT JOIN
into an INNER JOIN
. 否则,条件(甚至NOT IN
)将返回NULL
,从而将LEFT JOIN
变成INNER JOIN
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.