考考你 sql 和其他语言的思维上的区别

2019 年 2 月 3 日
 rootzeal
做做这道题试试
https://mp.weixin.qq.com/s/b7Y-ZnylzpbBsdEOMmtfFQ
2904 次点击
所在节点    数据库
10 条回复
yuikns
2019 年 2 月 3 日
sql 当然是自己 join 自己 然后 filter。

程序语言构造 set 然后 filter
rootzeal
2019 年 2 月 3 日
@yuikns 这有点复杂 可以考虑不用 join 的情况
FrailLove
2019 年 2 月 3 日
a b union all b a group by

sql 语言要用集合的方式思考问题
widewing
2019 年 2 月 3 日
取交集不用 inner join 用啥
jjplay
2019 年 2 月 3 日
大过年的,饶了我 8⃣️
mingyun
2019 年 2 月 3 日
哈哈,大过年的
yiyi11
2019 年 2 月 4 日
@FrailLove 老哥厉害了,sql 做统计真是神器。
yiyi11
2019 年 2 月 4 日
@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 年 2 月 4 日
用 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 里最普通的写法
rootzeal
2019 年 3 月 3 日
这里是 完整的回答 其实只要把 groupby 一下然后再 cout 过滤出 count 等于 2 的就行 不用 join https://mp.weixin.qq.com/s/VOfzuZ5OaG3imxpTcEM0EA

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://study.congcong.us/t/532845

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX