0

我有三个表,我想根据组排列过滤数据。

login

id | name     
-- | -------- 
1  | drew        
2  | john        
3  | alex         

group

id | group_name
-- | --------
1  | Scientist       
2  | Singer       
3  | Teacher       
4  | Dancer       
5  | Atty       
6  | IT       

group_member

id | group_id| login_id
-- | --------| ------
1  | 1       | 1
2  | 2       | 1
3  | 1       | 2  
4  | 3       | 2 
5  | 4       | 2 
6  | 3       | 3  
7  | 4       | 3

例如,如果我选择登录 1,如果 login_id 1 与其他登录具有相同的 group_id,则该组现在将显示。

login_id 1 有记录

group_id|group_name| login_id
--------| ------   | ------ 
   1    | Scientist| 1
   2    | Singer   | 1

所以如果我过滤组

结果将是

group_id|group_name| login_id
--------| ------   | ------ 
   3    | Teacher  | 2
   4    | Dancer   | 2
   3    | Teacher  | 3
   4    | Dancer   | 3
4

2 回答 2

0

如果我理解得很好,您可以使用它:

select group_id, group_name, login_id
from group_member join groups on
group_member.group_id = groups.id
where group_id not in 
   (select group_id from group_member
    where login_id=1)

请参阅小提琴示例

于 2013-09-13T06:52:44.640 回答
0
select g.id,g.group_name,m.login_id
from group_member m
join group g on m.group_id=g.id
where m.group_id not in (
select group_id
from group_member
where login_id=1)
于 2013-09-13T06:54:48.137 回答