[英]SQL query to select row from one table which is not in another table
Registraion and group_members 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
I need to get the names from registration table which are not a member in group_members table with Gid is 02 . 我需要从注册表中获取名称,该名称不是Gid 为02的 group_members表中的成员。 The output must be obviously A,B and D .
输出必须明显是A,B和D。 But I dont know how to achieve this.Please help.Thanks
但是我不知道该怎么实现,请帮忙。
Try this: 尝试这个:
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
This will filter out any matching records in group_members
table with Gid = '02'
. 这将滤除
group_members
表中group_members
Gid = '02'
所有匹配记录。
this should work 这应该工作
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.