返回介绍

solution / 1200-1299 / 1264.Page Recommendations / README_EN

发布于 2024-06-17 01:03:21 字数 3747 浏览 0 评论 0 收藏 0

1264. Page Recommendations

中文文档

Description

Table: Friendship

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| user1_id    | int   |
| user2_id    | int   |
+---------------+---------+
(user1_id, user2_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that there is a friendship relation between user1_id and user2_id.

 

Table: Likes

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| user_id   | int   |
| page_id   | int   |
+-------------+---------+
(user_id, page_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that user_id likes page_id.

 

Write a solution to recommend pages to the user with user_id = 1 using the pages that your friends liked. It should not recommend pages you already liked.

Return result table in any order without duplicates.

The result format is in the following example.

 

Example 1:

Input: 
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    |
+---------+---------+
Output: 
+------------------+
| recommended_page |
+------------------+
| 23         |
| 24         |
| 56         |
| 33         |
| 77         |
+------------------+
Explanation: 
User one is friend with users 2, 3, 4 and 6.
Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6.
Page 77 is suggested from both user 2 and user 3.
Page 88 is not suggested because user 1 already likes it.

Solutions

Solution 1: Union + Equi-Join + Subquery

First, we query all users who are friends with user_id = 1 and record them in the T table. Then, we query all pages that users in the T table like, and finally exclude the pages that user_id = 1 likes.

# Write your MySQL query statement below
WITH
  T AS (
    SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
    UNION
    SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
  )
SELECT DISTINCT page_id AS recommended_page
FROM
  T
  JOIN Likes USING (user_id)
WHERE page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);

Solution 2

# Write your MySQL query statement below
SELECT DISTINCT page_id AS recommended_page
FROM Likes
WHERE
  user_id IN (
    SELECT user1_id AS user_id FROM Friendship WHERE user2_id = 1
    UNION ALL
    SELECT user2_id AS user_id FROM Friendship WHERE user1_id = 1
  )
  AND page_id NOT IN (SELECT page_id FROM Likes WHERE user_id = 1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文