Makes Recommendations

题目地址:
http://www.1point3acres.com/bbs/thread-282889-1-1.html

题目:
Write an SQL query that makes recommendations using the pages that your friends liked. Assume you have two tables: a two-column table of users and their friends, and a two-column table of users and the pages they liked. It should not recommend pages you already like.

解题思路:
这道题直接写个subquery就好。这道题还有一个变种,就是找朋友的朋友who 不是我的朋友。

代码:

Select b.users, b.pages from pages_table b
Where b.users in (
        Select a.friends from friends_table a
        Where a.users = my_id
) and b.pages not in (
        select c.pages from pages_table c
        where c.users = my_id
)

Follow up:

SELECT friend_id
FROM FRIENDSHIPS
WHERE user_id IN
        (SELECT friend_ID FROM FRIENDSHIPS WHERE user_id=@YOURUSER_ID AND status=1)
AND friend_id NOT IN
        (SELECT friend_ID FROM FRIENDSHIPS WHERE user_id=@YOURUSER_ID AND status=1)
AND friend_id<>@YOURUSER_ID



Comments

Popular Posts