@FrailLove 老哥,这样对吗? select count (*) / 2 as '互关对数' from ( select uid as a, fid as b from t t1 union all select fid as a, uid as b form t t2 ) group by a, b having count(*) > 1
CRVV
2019-02-04 19:20:33 +08:00
用 SQL 至少有两种写法吧
SELECT count(*) / 2 AS pairs FROM user_relation a INNER JOIN user_relation b ON a.friend_uid = b.user_id WHERE a.user_id = b.friend_uid;
SELECT count(*) / 2 AS pairs FROM ( SELECT user_id, friend_uid FROM user_relation INTERSECT SELECT friend_uid, user_id FROM user_relation) AS subquery;