目录
题目
准备数据
分析数据
实现
题目
写出一个查询语句,找到具有最多共同关注者的所有两两结对组。换句话说,如果有两个用户的共同关注者是最大的,我们应该返回所有具有此最大值的两两结对组
结果返回表,每一行应该包含user1_id
和 user2_id,
其中user1_id < user2_id
.
返回结果 不要求顺序 。
准备数据
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')
分析数据
第一步:根据共同好友进行关联条件,并且确保a.user_id < b.user_id
select a.user_id as user1_id, b.user_id as user2_id, a.follower_id from Relations a join Relations b on a.follower_id = b.follower_id and a.user_id<b.user_id;
第二步:对共同好友统计个数
select user1_id, user2_id, count(*) as cnt from ( select a.user_id as user1_id, b.user_id as user2_id, a.follower_id from Relations a join Relations b on a.follower_id = b.follower_id and a.user_id<b.user_id ) as temp group by user1_id, user2_id;
第三步:筛选出具有这个最大值的用户对
with temp as ( select user1_id, user2_id, count(*) as cnt from ( select a.user_id as user1_id, b.user_id as user2_id, a.follower_id from Relations a join Relations b on a.follower_id = b.follower_id and a.user_id<b.user_id ) as temp group by user1_id, user2_id ) select user1_id, user2_id from temp where cnt = (select max(cnt) from temp);
实现
with temp as
(
select user1_id, user2_id, count(*) as cnt from
(
select a.user_id as user1_id, b.user_id as user2_id, a.follower_id from Relations a
join Relations b on a.follower_id = b.follower_id and a.user_id<b.user_id
) as temp
group by user1_id, user2_id
)
select user1_id, user2_id from temp
where cnt = (select max(cnt) from temp);