[英]Left Join and condition on Mysql
我想要这样一个查询,该查询返回表1 jointest1中的所有行以及与表2 jointest2中的连接条件匹配的行,但joinest2中的重复行除外
连接后的表应该看起来
查询将像
SELECT * FROM jointest1 LEFT JOIN jointest2 ON jointest1.id=jointest2.j1_id WHERE jointest2.id NOT IN ( 2 )
但是当我添加WHERE jointest2.id NOT IN ( 2 )
左WHERE jointest2.id NOT IN ( 2 )
不起作用时,它只是返回的行是jointest1.id=jointest2.j1_id and NOT IN ( 2 )
预先感谢您的帮助
left join
工作正常。 当您在第二张表上有条件时,它需要on
子句中:
SELECT *
FROM jointest1 LEFT JOIN
jointest2
ON jointest1.id = jointest2.j1_id AND
jointest2.id NOT IN ( 2 );
否则,条件(甚至NOT IN
)将返回NULL
,从而将LEFT JOIN
变成INNER JOIN
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.