1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-1951.test 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
# $1951. 查询具有最多共同关注者的所有两两结对组
# https://leetcode-cn.com/problems/all-the-pairs-with-the-maximum-number-of-common-followers/
# SQL架构
Create table If Not Exists Relations (user_id int, follower_id int);
Truncate table Relations;
insert into Relations (user_id, follower_id) values ('1', '3');
insert into Relations (user_id, follower_id) values ('2', '3');
insert into Relations (user_id, follower_id) values ('7', '3');
insert into Relations (user_id, follower_id) values ('1', '4');
insert into Relations (user_id, follower_id) values ('2', '4');
insert into Relations (user_id, follower_id) values ('7', '4');
insert into Relations (user_id, follower_id) values ('1', '5');
insert into Relations (user_id, follower_id) values ('2', '6');
insert into Relations (user_id, follower_id) values ('7', '5');
# Write your MySQL query statement below
select
tmp.user1_id,
tmp.user2_id
from
(
select
r1.user_id as user1_id,
r2.user_id as user2_id,
rank() over(
order by
count(*) desc
) rk
from
Relations r1,
Relations r2
where
r1.user_id < r2.user_id
and r1.follower_id = r2.follower_id
group by
r1.user_id,
r2.user_id
) tmp
where
tmp.rk = 1;
# clean-up
drop table Relations;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助