题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/page-recommendations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
本人思路:
– 1、朋友关系是相互的,所以使用union all(去掉重复的关系)连接两个方向的关系,即包含了所有的关系,构成了临时表temp
– 2、将temp表和likes表内连接,使用条件temp.user2_id =l.user_id,从而知道user1_id的朋友喜欢的界面;
– 3、where中放入筛选的行条件,以及“不要推荐该用户已经喜欢的页面”注意要使用not in 而不是 !=,因为会有未给出用户喜欢界面的情况
select distinct l.page_id as recommended_page
from(
select *
from Friendship
union all
select user2_id as user1_id,user1_id as user2_id
from Friendship
)temp inner join Likes as l
on temp.user2_id =l.user_id
where temp.user1_id = 1 and page_id not in (select distinct page_id from Likes where user_id = 1)