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

LeetCode_sql_day30(1264.页面推荐)

描述  1264.页面推荐

朋友关系列表: Friendship

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user1_id      | int     |
| user2_id      | int     |
+---------------+---------+
(user1_id, user2_id) 是这张表具有唯一值的列的组合。
这张表的每一行代表着 user1_id 和 user2_id 之间存在着朋友关系。

喜欢列表: Likes

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| page_id     | int     |
+-------------+---------+
(user_id, page_id) 是这张表具有唯一值的列的组合。
这张表的每一行代表着 user_id 喜欢 page_id。

编写解决方案,向user_id = 1 的用户,推荐其朋友们喜欢的页面。不要推荐该用户已经喜欢的页面。

以 任意顺序 返回结果,其中不应当包含重复项。

返回结果的格式如下例所示。

示例 1:

输入:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1        | 2        |
| 1        | 3        |
| 1        | 4        |
| 2        | 3        |
| 2        | 4        |
| 2        | 5        |
| 6        | 1        |
+----------+----------+Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1       | 88      |
| 2       | 23      |
| 3       | 24      |
| 4       | 56      |
| 5       | 11      |
| 6       | 33      |
| 2       | 77      |
| 3       | 77      |
| 6       | 88      |
+---------+---------+输出:
+------------------+
| recommended_page |
+------------------+
| 23               |
| 24               |
| 56               |
| 33               |
| 77               |
+------------------+
解释:
用户1 同 用户2, 3, 4, 6 是朋友关系。
推荐页面为: 页面23 来自于 用户2, 页面24 来自于 用户3, 页面56 来自于 用户3 以及 页面33 来自于 用户6。
页面77 同时被 用户2 和 用户3 推荐。
页面88 没有被推荐,因为 用户1 已经喜欢了它。

数据准备

Create table If Not Exists Friendship (user1_id int, user2_id int)
Create table If Not Exists Likes (user_id int, page_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 ('1', '4')
insert into Friendship (user1_id, user2_id) values ('2', '3')
insert into Friendship (user1_id, user2_id) values ('2', '4')
insert into Friendship (user1_id, user2_id) values ('2', '5')
insert into Friendship (user1_id, user2_id) values ('6', '1')
Truncate table Likes
insert into Likes (user_id, page_id) values ('1', '88')
insert into Likes (user_id, page_id) values ('2', '23')
insert into Likes (user_id, page_id) values ('3', '24')
insert into Likes (user_id, page_id) values ('4', '56')
insert into Likes (user_id, page_id) values ('5', '11')
insert into Likes (user_id, page_id) values ('6', '33')
insert into Likes (user_id, page_id) values ('2', '77')
insert into Likes (user_id, page_id) values ('3', '77')
insert into Likes (user_id, page_id) values ('6', '88')

分析

①首先找出用户编号为1 的朋友

提供两种方法 

第一种union 

select user2_idfrom Friendshipwhere user1_id = 1unionselect user1_idfrom Friendshipwhere user2_id = 1

第二种case when

select casewhen user1_id = 1 then user2_idwhen user2_id = 1 then user1_id end friends
from Friendship

② 查询Likes表 条件user_id要在第①问所得出的表种 同时 还不能包括自己(user_id=1)的

select distinct page_id
from Likes
where user_id in (select user2_idfrom Friendshipwhere user1_id = 1unionselect user1_idfrom Friendshipwhere user2_id = 1)and page_id not in (select page_id from Likes where user_id = 1

代码

# 法一
select distinct page_id
from Likes
where user_id in (select user2_idfrom Friendshipwhere user1_id = 1unionselect user1_idfrom Friendshipwhere user2_id = 1)and page_id not in (select page_id from Likes where user_id = 1);#法二
select distinct page_id
from Likes
where user_id in (select casewhen user1_id = 1 then user2_idwhen user2_id = 1 then user1_id end friendsfrom Friendship)and page_id not in (select page_id from Likes where user_id = 1);

总结

case when 的用法 很奇妙 可以积累一下

遇到涉及到两列的数据 可以考虑使用union  和 case  when

http://www.lryc.cn/news/442374.html

相关文章:

  • mysql通过binlog做数据恢复
  • macos清理垃圾桶时提示 “操作无法完成,因为该项目正在使用中” 解决方法 , 强制清理mac废纸篓 方法
  • vue3 axios ant-design-vue cdn的方式使用
  • neo4j导入csv数据
  • YOLOV8实现小目标检测
  • 解决 Prettier ESLint 错误
  • 百度网盘企业版数据快速上云,数据流转平台 CloudFlow 加速大模型训练迭代
  • 地面站通过SSH连接无人机
  • 【Pytorch】大语言模型中的CrossEntropyLoss
  • 安全热点问题
  • C++——用选择法对10个数值进行排序。
  • CSP-CCF★★★201909-2小明种苹果(续)★★★
  • 硬件工程师笔试面试——变压器
  • Visual Studio Code( VS Code)倍速提高编程工作效率的免费的源代码编辑器
  • 华为SMU02B1智能通信电源监控单元模块简介
  • 【刷题日记】15. 三数之和
  • 低级编程语言和高级编程语言
  • Spring Boot-API网关问题
  • 三 auto占位符
  • tail: inotify 资源耗尽
  • 什么是损失函数?常见的损失函数有哪些?
  • Python Web 开发中的国际化与本地化处理
  • android API、SDK与android版本
  • OpenHarmony(鸿蒙南向开发)——小型系统内核(LiteOS-A)【内核通信机制】下
  • 如何联系真正的开发者而非公司??
  • OpenCV运动分析和目标跟踪(1)累积操作函数accumulate()的使用
  • source ~/.bash_profile有什么用
  • 【C++笔记】类和对象的深入理解(三)
  • 时代变了,MySQL 早已不是最流行的数据库了
  • K8S容器实例Pod安装curl-vim-telnet工具