表: Relations+-------------+------+| Column Name | Type |+-------------+------+| user_id | int || follower_id | int |+-------------+------+(user_id, follower_id) 是这个表的主键.这个表的每一行,表示这个user_id的用户和他的关注者,关注者的id 就是本表的 user_id.写出一个查询语句,找到具有最多共同关注者的所有两两结对组。换句话说,如果有两个用户的共同关注者是最大的,我们应该返回所有具有此最大值的两两结对组结果返回表,每一行应该包含user1_id和?user2_id,其中user1_id < user2_id.返回结果不要求顺序。查询结果格式如下例:Relations 表:+---------+-------------+| user_id | follower_id |+---------+-------------+| 1 | 3 || 2 | 3 || 7 | 3 || 1 | 4 || 2 | 4 || 7 | 4 || 1 | 5 || 2 | 6 || 7 | 5 |+---------+-------------+Result 表:+----------+----------+| user1_id | user2_id |+----------+----------+| 1 | 7 |+----------+----------+用户1 和用户 2 有2个共同的关注者(3和4)。用户1 和用户 7 有3个共同的关注者(3,4和5)。用户2 和用户7 有2个共同的关注者(3和4)。既然两两结对的所有组队的最大共同关注者的数值是3,所以,我们应该返回所有拥有3个共同关注者的两两组队,这就是仅有的一对(1, 7).我们返回的是(1, 7).,而不是(7, 1).注意,我们没有关于用户3,4,5的任何关注者信息,我们认为他们有0个关注者。来源:力扣(LeetCode)链接:https://leetcode.cn/problems/all-the-pairs-with-the-maximum-number-of-common-followers
#测试数据Create table If Not Exists Relations (user_id int, follower_id int);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');

selecta.user_id,b.user_id,count(*) cnfrom Relations ainner join Relations bon a.follower_id = b.follower_idgroup by a.user_id,b.user_id;
selectuser1_id,user2_idfrom (selectuser1_id,user2_id,rank() over(order by cn desc) rkfrom(selecta.user_id user1_id,b.user_id user2_id,count(*) cnfrom Relations ainner join Relations bon a.user_id < b.user_idand a.follower_id = b.follower_idgroup by a.user_id,b.user_id)a)bwhere b.rk = 1;

文章转载自跟强哥学SQL,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




