简体   繁体   中英

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

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 )

Thanks in advance for help

The left join is working fine. When you have a condition on the second table, it needs to go in the on clause:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM