描述 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