查询:相关子查询的朋友
select friends_of_first.friend
from (
select id2 as friend from friend_relastionship where id1 = '4'
union
select id1 as friend from friend_relastionship where id2 = '4'
) friends_of_first
join (
select id2 as friend from friend_relastionship where id1 = '7'
union
select id1 as friend from friend_relastionship where id2 = '7'
) friends_of_second
on friends_of_first.friend = friends_of_second.friend;
此查询查找用户4和7
我想用这个作为查找表friend_relastionship
内的所有对共同的朋友的基础之间的共同的朋友以便我可以选择最具共同朋友的顶级对。我的理解是,我可以在与相关子查询的每个配对上运行此操作,但我不确定如何操作。
该表的设计方式是:id1 < id2,如果友谊存在于1和7之间,那么它被列为1,7和从不7,1。所以友谊出现一次。
这里是一个sqlfiddle:http://sqlfiddle.com/#!2/48eb0/1
在这种sqlfiddle它应该显示
USER1 USER2 COUNT
3 4 3
6 7 2
4 45 2
2 7 2
2 6 2
1 2 2
1 45 2
0 2 2
...
这表明3和4应该是朋友,因为他们有3周共同的朋友。
什么是连接? – ParoX 2014-10-06 15:18:34
连接是id1和id2共有的朋友。 – JLampon 2014-10-06 17:08:47
我看不到它来自哪里。我知道连接是我postesd查询的结果,但我如何让这个查询在每个组合上运行? – ParoX 2014-10-06 17:41:22