有两个表
现在当用户" xyz"访问用户" abc" Abc的朋友列表应该将共同的朋友和朋友分类为abc ..
关键字中的"用户"表的结构 列(id,user)值(1,xyz)(2,abc),(3,lmn),(4,kmp)
关系的表结构: 列(发送者,接受者,行动)值(1,2,1),(1,3,1)(2,3,1)(2,4,1)
值(1,2,1)意味着用户1和2是朋友吗? 行动1代表有朋友
我想要一个拥有"所有共同朋友"和" abc的所有朋友都不是共同的朋友"
我希望我向任何帮助表示赞赏
答案 0 :(得分:0)
用户xyz的朋友是:
select sender from relation where acceptor = (select id from user where user = 'xyz')
union
select acceptor from relation where sender = (select id from user where user = 'xyz');
您也可以通过查询一次表来获取它们:
select distinct case when r.sender = u.id then r.acceptor else r.sender end
from user u
join relation r on u.id in (r.sender, r.acceptor)
where u.user = 'xyz';
现在使用上述查询之一我们可以获得abc的朋友以及xyz的朋友。通过将xyz的朋友外接到abc的朋友,我们可以确定abc的朋友是否是共同的朋友。
select
friends_of_abc.friend,
friends_of_xyz.friend is not null as is_a_mutual_friend
from
(
select distinct case when r.sender = u.id then r.acceptor else r.sender end as friend
from user u
join relation r on u.id in (r.sender, r.acceptor)
where u.user = 'abc'
) friends_of_abc
left join
(
select distinct case when r.sender = u.id then r.acceptor else r.sender end as friend
from user u
join relation r on u.id in (r.sender, r.acceptor)
where u.user = 'xyz'
) friends_of_xyz on friends_of_xyz.friend = friends_of_abc.friend;