簡體   English   中英

左聯接和條件在MySQL

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM