LeetCode(数据库)- 好友申请II:谁有最多的好友
Posted Lux_Sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 好友申请II:谁有最多的好友相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:略。
AC 代码
-- 解决方案(1)
select ids as id, cnt as num
from
(
select ids, count(*) as cnt
from
(
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted
) as tbl1
group by ids
) as tbl2
order by cnt desc
limit 1;
-- 解决方案(2)
WITH t1 AS(SELECT requester_id id, COUNT(requester_id) cnt
FROM request_accepted
GROUP BY requester_id),
t2 AS(SELECT accepter_id id, COUNT(accepter_id) cnt
FROM request_accepted
GROUP BY accepter_id),
t3 AS(SELECT * FROM t1
UNION ALL
SELECT * FROM t2)
SELECT id, SUM(cnt) num
FROM t3
GROUP BY id
ORDER BY num DESC
LIMIT 1
以上是关于LeetCode(数据库)- 好友申请II:谁有最多的好友的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 602. Friend Requests II: Who Has Most Friend? 朋友请求 II: 谁有最多的朋友?