1
yuikns 2019-02-03 15:45:05 +08:00 via iPad
sql 当然是自己 join 自己 然后 filter。
程序语言构造 set 然后 filter |
3
FrailLove 2019-02-03 19:10:25 +08:00 via iPhone 1
a b union all b a group by
sql 语言要用集合的方式思考问题 |
4
widewing 2019-02-03 19:21:31 +08:00 via Android
取交集不用 inner join 用啥
|
5
jjplay 2019-02-03 20:12:27 +08:00 via iPhone
大过年的,饶了我 8⃣️
|
6
mingyun 2019-02-03 22:36:43 +08:00 via Android
哈哈,大过年的
|
8
yiyi11 2019-02-04 13:22:27 +08:00 via Android
@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 |
9
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; 个人认为 JOIN 是 SQL 里最普通的写法 |
10
rootzeal OP 这里是 完整的回答 其实只要把 groupby 一下然后再 cout 过滤出 count 等于 2 的就行 不用 join https://mp.weixin.qq.com/s/VOfzuZ5OaG3imxpTcEM0EA
|