当前位置: 首页 > article >正文

MySQL高阶1949-坚定地友谊

目录

题目

准备数据

分析数据

实现


题目

如果 x  和 y 为 朋友 且他们 至少 有三个共同的朋友 ,那么 x 和 y 之间的友谊就是 坚定的

写一个解决方案来找到所有的 坚定的友谊

注意,结果表不应该包含重复的行,并且 user1_id < user2_id

以 任何顺序 返回结果表。

准备数据

Create table If Not Exists Friendship (user1_id int, user2_id int)
Truncate table Friendship
insert into Friendship (user1_id, user2_id) values ('1', '2')
insert into Friendship (user1_id, user2_id) values ('1', '3')
insert into Friendship (user1_id, user2_id) values ('2', '3')
insert into Friendship (user1_id, user2_id) values ('1', '4')
insert into Friendship (user1_id, user2_id) values ('2', '4')
insert into Friendship (user1_id, user2_id) values ('1', '5')
insert into Friendship (user1_id, user2_id) values ('2', '5')
insert into Friendship (user1_id, user2_id) values ('1', '7')
insert into Friendship (user1_id, user2_id) values ('3', '7')
insert into Friendship (user1_id, user2_id) values ('1', '6')
insert into Friendship (user1_id, user2_id) values ('3', '6')
insert into Friendship (user1_id, user2_id) values ('2', '6')

分析数据

第一步:

UNION ALL合并,UNION ALL不会消除重复的行,意味着如果user1_iduser2_id互为好友,那么这两行都会被包含。并且按照user_idfriend_id的顺序。

select user1_id user_id,user2_id friend_id
from Friendship
union all
select user2_id,user1_id
from Friendship
order by 1,2;

第二步:

  • where (t1.user_id, t2.user_id) in (select * from t):确保t1t2中的user_id对在CTE t中存在。
  • and t1.friend_id = t2.friend_id:确保t1t2有相同的friend_id,即它们有共同的好友。
  • and t1.user_id < t2.user_id:确保user1_id小于user2_id,避免重复的组合(例如,用户1和用户2与用户2和用户1被视为相同的组合)。
  • group by 1, 2:按user1_iduser2_id分组。
  • having count(distinct t1.friend_id) >= 3:筛选出至少有3个不同共同好友的用户对。
with t as (
    (select user1_id user_id,user2_id friend_id
     from Friendship
     union all
     select user2_id,user1_id
     from Friendship)
        order by 1,2
)
select *
from t t1,t t2
where (t1.user_id,t2.user_id) in (select * from t)
    and t1.friend_id = t2.friend_id
  and t1.user_id < t2.user_id
order by 1,2;

第三步:

  • select t1.user_id user1_id, t2.user_id user2_id, count(distinct t1.friend_id) common_friend:选择两列user1_iduser2_id,以及它们之间不同的共同好友数量common_friend
with t as (
    (select user1_id user_id,user2_id friend_id
     from Friendship
     union all
     select user2_id,user1_id
     from Friendship)
        order by 1,2
)
select t1.user_id user1_id
     ,t2.user_id user2_id
     ,count(distinct t1.friend_id) common_friend
from t t1,t t2
where
    (t1.user_id,t2.user_id) in (select * from t)
  and t1.friend_id = t2.friend_id
  and t1.user_id < t2.user_id
group by 1,2
having count(distinct t1.friend_id) >=3;

实现

with t as (
    (select user1_id user_id,user2_id friend_id
     from Friendship
     union all
     select user2_id,user1_id
     from Friendship)
        order by 1,2
)
select t1.user_id user1_id
     ,t2.user_id user2_id
     ,count(distinct t1.friend_id) common_friend
from t t1,t t2
where
    (t1.user_id,t2.user_id) in (select * from t)
  and t1.friend_id = t2.friend_id
  and t1.user_id < t2.user_id
group by 1,2
having count(distinct t1.friend_id) >=3;


http://www.kler.cn/a/319738.html

相关文章:

  • 一文了解Android中的AudioFlinger
  • 群控系统服务端开发模式-应用开发-前端登录接口开发
  • idea 弹窗 delete remote branch origin/develop-deploy
  • git没有识别出大写字母改成小写重命名的文件目录
  • 安全见闻1-5
  • 阿里云centos7.9服务器磁盘挂载,切换服务路径
  • 查询最近正在执行的sql(DM8 : 达梦数据库)
  • 【艾思科蓝】Spring Boot实战:零基础打造你的Web应用新纪元
  • 漫谈 Kubernetes 的本质
  • 网络安全:腾讯云智、绿盟、美团、联想的面经
  • [WMCTF2020]Make PHP Great Again 2.01
  • 使用 Python 绘制 BTC 期权的波动率曲面
  • ③无需编程 独立通道 Modbus主站EtherNet/IP转ModbusRTU/ASCII工业EIP网关串口服务器
  • 单词搜索问题(涉及递归等)
  • docker多阶段镜像制作,比如nginx镜像,编译+制作
  • 【SpringBoot整合Redis测试Redis集群案例】
  • 【QT 5 调试软件+Linux下调用脚本shell-无法调度+目录拼写+无法找目录+sudo权限(2)+问题解决方式+后续补充】
  • linux网络编程8
  • 【JavaScript】算法之贪心算法(贪婪算法)
  • C++之文件操作
  • 考虑电网交互及禁止运行区的风电、光伏与火电互补调度运行(MATLAB-Yalmip-Cplex全代码)
  • uniapp webview清理缓存
  • 华为云徐峰:AI赋能应用现代化,加速软件生产力跃升
  • 聚合函数count 和 group by
  • 【linux】进度条
  • 常见服务端口号和中文大全