leetcode 1264页面推荐(postgresql)

需求

朋友关系列表: 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。

写一段 SQL 向user_id = 1 的用户,推荐其朋友们喜欢的页面。不要推荐该用户已经喜欢的页面。

你返回的结果中不应当包含重复项。

输入

输出

sql 复制代码
with t1 as (select case
                       when user1_id = 1 then user2_id
                       else user1_id
                       end as fri_id
            from friendship
            where user1_id = 1
               or user2_id = 1)
select distinct page_id
from t1,
     likes
where t1.fri_id = Likes.user_id
  and Likes.page_id != (select page_id from likes where user_id = 1)
order by page_id;
相关推荐
noravinsc20 分钟前
connection.cursor() 与 models.objects.filter
数据库·django·原生查询·orm查询
wuqingshun31415934 分钟前
蓝桥杯 2. 确定字符串是否是另一个的排列
数据结构·c++·算法·职场和发展·蓝桥杯
laimaxgg2 小时前
MySQL复合查询
数据库·mysql
编程在手天下我有2 小时前
Redis 常见问题深度剖析与全方位解决方案指南
数据库·redis·缓存·性能优化·数据持久化·分布式系统
1白天的黑夜12 小时前
贪心算法-2208.将数组和减半的最小操作数-力扣(LeetCode)
c++·算法·leetcode·贪心算法
Dream it possible!3 小时前
LeetCode 热题 100_最小路径和(92_64_中等_C++)(多维动态规划)
c++·leetcode·动态规划
vim怎么退出3 小时前
46.二叉树展开为链表
前端·leetcode
C语言魔术师3 小时前
62.不同路径
算法·leetcode·动态规划
fantasy_43 小时前
LeetCode238☞除自身以外数组的乘积
java·数据结构·python·算法·leetcode
辰哥单片机设计3 小时前
JQ6500语音模块详解(STM32)
数据库·mongodb