给出三个数据表,theUser
,theGroup
,membership
:
theUser:(*user_id*, username),
theGroup:(*group_id*, groupname),
membership:(*group_id*, *user_id*)
例如,小组2
有用户007
和008
,membership
应该是这样的:
group_id user_id
2 007
2 008
现在我希望选择用户008
尚未进入的组。你能告诉我怎么做吗?提前致谢!
答案 0 :(得分:2)
有几种不同的方法可以做到这一点。这是一张outer join / null
支票:
select g.*
from thegroup g
left join membership m on g.groupid = m.groupid and m.userid = '008'
where m.groupid is null
答案 1 :(得分:1)
试试这个:
select distinct group_id
from theGroup
where group_id not in
(select group_id from membership where user_id='008');
答案 2 :(得分:-1)
SELECT * FROM theGroup
WHERE group_id
NOT IN(SELECT group_id
FROM membership
WHERE user_id
LIKE' 008')