LeetCode-1264. Page recommended (medium)

Friends list of relationship: Friendship

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| user1_id | int |
| user2_id | int |
+ --------------- + --------- +
this table the primary key is (user1_id, user2_id).
Each row represents this table there is a relationship between friends and user1_id user2_id.
 

Like the list: Likes

+ ------------- + --------- +
| Column the Name | Type |
+ ------------- + ---- + -----
| user_id | int |
| page_id | int |
+ ------------- + --------- +
primary key of this table is (user_id, page_id).
User_id Each row represents this table like page_id.
 

SQL to write a user user_id = 1, it is recommended that friends like the page. Do not recommended that the user has liked the page.

Your results returned should not contain duplicates.

Returns the result of the format shown in the following example:

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      |
+---------+---------+

The Table the Result:
+ ------------------ +
| recommended_page |
+ ------------------ +
| 23 |
| 24 |
| 56 is |
| 33 is |
| 77 |
+ ------------------ +
users with user 1 2, 3, 4, 6 is a friend.
Recommended page is: page 23 from the user 2, user 3 from page 24, page 56 and page 3 from the user 33 from the user 6.
Page 77 while being recommended users 2 and 3.
Page 88 is not recommended, because the user has 1 liked it.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/page-recommendations
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation:

Thinking:

First screened twice user_id, after screening out the numbers 2,3,4,6 partners, No. 88, No. 6 excluded partner likes.

Problem solving:

select distinct page_id as recommended_page
from Likes,friendship
where page_id
not in
(select page_id from likes where user_id=1)
and
( user_id in (select user1_id from friendship where user2_id=1)
or
user_id in (select user2_id from friendship where user1_id=1));

Method Two:

SELECT DISTINCT(page_id) AS recommended_page 
FROM Likes 
WHERE (user_id IN (
    SELECT user2_id
    FROM Friendship
    WHERE user1_id = 1 
) OR user_id IN (
    SELECT user1_id
    FROM Friendship
    WHERE user2_id = 1
))
AND page_id NOT IN (
    SELECT page_id AS liked_ids 
    FROM Likes
    WHERE user_id  = 1

 Knowledge points:

Published 144 original articles · won praise 2 · Views 5740

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104749872