[英]sql select query for one row in one table and multiple row from another table
[英]SQL query to select row from one table which is not in another table
Registraion和group_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.