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

MySQL高阶1917-Leetcodify好友推荐

目录

题目

准备数据

分析数据

总结


题目

为 Leetcodify 用户推荐好友。我们将符合下列条件的用户 x 推荐给用户 y :

  • 用户 x 和 y 不是好友,且
  • 用户 x 和 y 在同一天收听了相同的三首或更多不同歌曲。

注意,好友推荐是单向的,这意味着如果用户 x 和用户 y 需要互相推荐给对方,结果表需要将用户 x 推荐给用户 y 并将用户 y 推荐给用户 x。另外,结果表不得出现重复项(即,用户 y 不可多次推荐给用户 x )。

任意顺序返回结果表。

准备数据

Create table If Not Exists Listens (user_id int, song_id int, day date);
Create table If Not Exists Friendship (user1_id int, user2_id int);Truncate table Listens;insert into Listens (user_id, song_id, day) values ('1', '10', '2021-03-15');insert into Listens (user_id, song_id, day) values ('1', '11', '2021-03-15');insert into Listens (user_id, song_id, day) values ('1', '12', '2021-03-15');insert into Listens (user_id, song_id, day) values ('2', '10', '2021-03-15');insert into Listens (user_id, song_id, day) values ('2', '11', '2021-03-15');insert into Listens (user_id, song_id, day) values ('2', '12', '2021-03-15');insert into Listens (user_id, song_id, day) values ('3', '10', '2021-03-15');insert into Listens (user_id, song_id, day) values ('3', '11', '2021-03-15');insert into Listens (user_id, song_id, day) values ('3', '12', '2021-03-15');insert into Listens (user_id, song_id, day) values ('4', '10', '2021-03-15');insert into Listens (user_id, song_id, day) values ('4', '11', '2021-03-15');insert into Listens (user_id, song_id, day) values ('4', '13', '2021-03-15');insert into Listens (user_id, song_id, day) values ('5', '10', '2021-03-16');insert into Listens (user_id, song_id, day) values ('5', '11', '2021-03-16');insert into Listens (user_id, song_id, day) values ('5', '12', '2021-03-16');Truncate table Friendship;insert into Friendship (user1_id, user2_id) values ('1', '2');

listens表

friendship表

分析数据

SELECT DISTINCT t.user1_id AS user_id,t.user2_id AS recommended_id
FROM(SELECT a.user_id AS user1_id,b.user_id AS user2_id,a.song_id,a.day,COUNT(a.song_id) OVER (PARTITION BY a.day,a.user_id,b.user_id) AS cntFROM (SELECT DISTINCT * FROM Listens) aINNER JOIN (SELECT DISTINCT * FROM Listens) bON a.user_id <> b.user_idAND a.song_id = b.song_idAND a.day = b.day) tLEFT JOIN Friendship t1ON t.user1_id = t1.user1_id AND t.user2_id = t1.user2_idLEFT JOIN Friendship t2ON t.user1_id = t2.user2_id AND t.user2_id = t2.user1_id
WHERE t.cnt >= 3 AND t1.user1_id IS NULL AND t2.user1_id IS NULL;

其中

分析一:对listens自连接,让user_id不相等,song_id和day相等,最后利用开窗函数,统计出个数

SELECT a.user_id AS user1_id,b.user_id AS user2_id,a.song_id,a.day,COUNT(a.song_id) OVER (PARTITION BY a.day,a.user_id,b.user_id) AS cnt
FROM (SELECT DISTINCT * FROM Listens) aINNER JOIN (SELECT DISTINCT * FROM Listens) bON a.user_id <> b.user_idAND a.song_id = b.song_idAND a.day = b.day;

分析二:再将得到的表和friendship 进行两次左连接,第一次是查找已经存在友谊关系的用户组;第二次查找反向的友谊关系

SELECT *
FROM(SELECT a.user_id AS user1_id,b.user_id AS user2_id,a.song_id,a.day,COUNT(a.song_id) OVER (PARTITION BY a.day,a.user_id,b.user_id) AS cntFROM (SELECT DISTINCT * FROM Listens) aINNER JOIN (SELECT DISTINCT * FROM Listens) bON a.user_id <> b.user_idAND a.song_id = b.song_idAND a.day = b.day) tLEFT JOIN Friendship t1ON t.user1_id = t1.user1_id AND t.user2_id = t1.user2_idLEFT JOIN Friendship t2ON t.user1_id = t2.user2_id AND t.user2_id = t2.user1_id;

分析三:只保留那些共同听过至少3次相同歌曲的用户组,并且在friendship表中没记录的

SELECT *
FROM(SELECT a.user_id AS user1_id,b.user_id AS user2_id,a.song_id,a.day,COUNT(a.song_id) OVER (PARTITION BY a.day,a.user_id,b.user_id) AS cntFROM (SELECT DISTINCT * FROM Listens) aINNER JOIN (SELECT DISTINCT * FROM Listens) bON a.user_id <> b.user_idAND a.song_id = b.song_idAND a.day = b.day) tLEFT JOIN Friendship t1ON t.user1_id = t1.user1_id AND t.user2_id = t1.user2_idLEFT JOIN Friendship t2ON t.user1_id = t2.user2_id AND t.user2_id = t2.user1_id
WHERE t.cnt >= 3 AND t1.user1_id IS NULL AND t2.user1_id IS NULL;

分析四:去重,选择唯一的用户组

SELECT DISTINCT t.user1_id AS user_id,t.user2_id AS recommended_id
FROM(SELECT a.user_id AS user1_id,b.user_id AS user2_id,a.song_id,a.day,COUNT(a.song_id) OVER (PARTITION BY a.day,a.user_id,b.user_id) AS cntFROM (SELECT DISTINCT * FROM Listens) aINNER JOIN (SELECT DISTINCT * FROM Listens) bON a.user_id <> b.user_idAND a.song_id = b.song_idAND a.day = b.day) tLEFT JOIN Friendship t1ON t.user1_id = t1.user1_id AND t.user2_id = t1.user2_idLEFT JOIN Friendship t2ON t.user1_id = t2.user2_id AND t.user2_id = t2.user1_id
WHERE t.cnt >= 3 AND t1.user1_id IS NULL AND t2.user1_id IS NULL;

总结

对于这种想要反向关系,可以对表进行自连接.


http://www.mrgr.cn/news/33296.html

相关文章:

  • Struts扫盲
  • <项目代码>YOLOv8 草莓成熟识别<目标检测>
  • 标准C++ 字符串
  • hive的tblproperties支持修改的属性
  • 国标GB28181视频平台EasyCVR私有化部署视频平台对接监控录像机NVR时,录像机“资源不足”是什么原因?
  • 低代码、配置式web组态软件
  • [图解]静态关系和动态关系
  • 使用GPU 加速 Polars:高效解决大规模数据问题
  • js之遍历方法
  • 【多模态大模型】Qwen2-VL基本原理和推理部署实战
  • 为什么 AVIF 将成为下一代图片格式之王
  • 基于Python的自然语言处理系列(16):TorchText + CNN + Teacher Forcing
  • 照片EXIF数据统计与可视化
  • [数据结构与算法·C++] 笔记 1.5类与对象
  • 力扣之183.从不订购的客户
  • 软件测试APP测试过程中的关键步骤、工具使用及常见问题处理方法。
  • MySQL篇(事务 - 基础)
  • 【LLM】Ollama:本地大模型使用
  • 从编辑器到自动化脚本,提高编程效率的必备工具秘籍
  • 【C语言】const char*强制类型转换 (type cast)的告警问题
  • Qt 每日面试题 -2
  • react:React Hook函数
  • 华为OD机试真题-IPv4地址转换成整数-2024年OD统一考试(E卷)
  • 打开C嘎嘎的大门:你好,C嘎嘎!(2)
  • 语言RPA流程组件介绍--获取网页信息
  • 上位机图像处理和嵌入式模块部署(linux小系统开发)