[英]mysql joins exlude matching records in another table
我正在開發一個網站,我必須向登錄的用戶顯示隨機的用戶個人資料(不包括已連接的成員)。 我有一個成員表,其中包含字段為memberid,firstname和lastname。 我有另一個用於連接的表,其中的字段為memberid和friendid。
現在,當我在連接表上使用左聯接時 ,我得到的只是我不想連接的成員的配置文件。 我只想顯示未與登錄用戶連接的個人資料表單成員表
您可以使用Not in
使用Join
的情況下實現此目的。
select *from
members
where
members.memberid not in (select memberid
from connections
)
;
上面的查詢只是意味着顯示不在連接中的成員ID的信息。
編輯:
由於您已經使用過Left join
。 這是使用Left join
實現此任務的查詢:
select m.*
from members m
left join connections c
on m.id = c.id
where c.id is null;
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.