friend :
UID FUID
1234 2222
1234 3333
1234 4444
2222 1234
2222 3333
3333 1234
3333 5555
4444 1234
4444 2222
5555 2222
5555 3333
6666 2222
6666 3333
6666 4444
即id=1234的好友集是2222,3333和4444。我怎样才能得到只包含6666的报告? 感谢您! 这个问题可以用“NOT EXISTS”和“MINUS”来解决吗?
以下是我的尝试:
select
f1.uid
from friend f1
minus
select
f2.uid
from friend f2
where not exists
(select
f.fuid
from friend1 f
where f.uid=1234);
最佳答案
这个想法是对 friend 进行自加入并查找所有匹配项。
以下内容匹配好友,然后按uid
分组。计数表明所有 friend 都匹配:
select f.uid
from (select f1234.*, count(*) over () as numfriends
from friend f1234
where f1234.uid = 1234
) f1234 left join
friend f
on f.fuid = f1234.fuid and
f.uid <> 1234
group by f.uid, f1234.numfriends
having count(*) = f1234.numfriends;
如果您想要完全匹配(意味着没有额外的 friend ),那么您可以使用完全外连接
。
关于sql - 如何获取某个用户拥有同一组好友的uid? (SQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186980/