Ai
1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
lc-1951.test 1.39 KB
Copy Edit Raw Blame History
逸扬 authored 2022-04-27 00:22 +08:00 . 1532-1596-1613-1709-1767-1831-1949-1951-1972 (9)
# $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

Search