[英]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.