Mysql SQL查询,从一个表中选择不在另一个表中的行
注册和集团成员Mysql SQL查询,从一个表中选择不在另一个表中的行,mysql,join,Mysql,Join,注册和集团成员 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。 输出必须明显A、B和D。 但我不知道如何做到这一点。请帮助。谢谢尝试
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。
输出必须明显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\u members
表中带有Gid='02'
的所有匹配记录,请尝试以下操作:
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\u 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')
这应该行得通
select name from
Registration reg where
not exists
(select null
from group_members gm
where gm.name = reg.name
and gm.gid = '02')