簡體   English   中英

SQL查詢從一個表中選擇不在另一表中的行

[英]SQL query to select row from one table which is not in another table

Registraiongroup_members

Registration Table
id  name  
-------                
1   A    
2   B    
3   C
4   D

group_members Table
name  Gid 
-------                
A   01    
B   01    
C   02

我需要從注冊表中獲取名稱,該名稱不是Gid 為02的 group_members表中的成員。 輸出必須明顯是A,B和D。 但是我不知道該怎么實現,請幫忙。

嘗試這個:

SELECT t1.*
FROM Registration AS t1
LEFT JOIN Group_members AS t2 ON t1.name = t2.name AND t2.Gid = '02'
WHERE t2.name IS NULL 

這將濾除group_members表中group_members Gid = '02'所有匹配記錄。

這應該工作

select name from
Registration reg where 
not exists 
(select null 
from group_members gm
where gm.name = reg.name
and gm.gid = '02')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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