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;
相关推荐
kura_tsuki3 小时前
[Oracle数据库] Oracle 常用函数
数据库·oracle
YA3334 小时前
java基础(十)sql的mvcc
数据库
weixin_307779138 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
无聊的小坏坏8 小时前
拓扑排序详解:从力扣 207 题看有向图环检测
算法·leetcode·图论·拓扑学
SelectDB8 小时前
Apache Doris 4.0 AI 能力揭秘(一):AI 函数之 LLM 函数介绍
数据库·人工智能·数据分析
我是哈哈hh9 小时前
【MySQL】在UBuntu环境安装以及免密码登录入门
linux·数据库·mysql·ubuntu
HeyZoeHey9 小时前
Mybatis执行sql流程(一)
java·sql·mybatis
喪彪10 小时前
MySQL新手教学
数据库·mysql·adb
浮灯Foden11 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表