日期:2014-05-17  浏览次数:20625 次

如何高效查询SNS中与用户是互粉的用户id号和个数
希望大虾们帮帮我这个小菜鸟,谢谢


fromuserid 表示关注者用户id 
touserid 表示被关注用户id


------解决方案--------------------
请问你想要的结果是什么?举个例子.

------解决方案--------------------

with test as 
(

select 1 id , 1 fromid,2 toid union 
select 2 id , 1 fromid,3 toid union 
select 3 id , 1 fromid,4 toid union 
select 4 id , 1 fromid,5 toid union 
select 5 id , 2 fromid,1 toid union 
select 6 id , 2 fromid,3 toid union 
select 7 id , 2 fromid,4 toid union 
select 8 id , 3 fromid,2 toid union 
select 9 id , 3 fromid,6 toid 
)
select  a.*
from test as a join test as b on a.toid = b.fromid and a.fromid = b.toid