SQL用于简单的推荐系统

时间:2018-03-15 17:41:42

标签: sql

电影表中的每条记录都有:

用户 - 用户ID
电影 - 电影ID
评分 - 电影的用户评分

用户 1 想要从 2,3,4,5,6,7 的用户组中接收推荐(电影列表)。
不应在用户1的电影中找到推荐的电影,并且该组的平均评分应不低于 3 。同时,不推荐观看不到3人观看的电影。

这是我的选择,但它并不反映最后一项要求:

SELECT t2.movie
FROM movies t1 INNER JOIN movies t2
     ON t1.user = 1
     AND t2.user IN(2,3,4,5,6,7)
     AND t2.movie NOT IN ( SELECT movie
                           FROM movies
                           WHERE user = 1 )
GROUP BY(t2.movie)
HAVING AVG(t2.rating)>=3

2 个答案:

答案 0 :(得分:0)

SELECT t2.movie
FROM movies t1 INNER JOIN movies t2
    ON t1.user = 1
       AND t2.user IN(2,3,4,5,6,7)
       AND t2.movie NOT IN ( SELECT movie
                             FROM movies
                            WHERE user = 1 )
WHERE (select count(distinct t3.user) from movies t3 
         where t3.movie = t2.movie) >= 3
GROUP BY(t2.movie)
HAVING AVG(t2.rating)>=3

答案 1 :(得分:0)

这不起作用吗?

SELECT t2.movie
FROM movies t1 INNER JOIN movies t2
     ON t1.user = 1
     AND t2.user IN(2,3,4,5,6,7)
     AND t2.movie NOT IN ( SELECT movie
                           FROM movies
                           WHERE user = 1 )
GROUP BY(t2.movie)
HAVING AVG(t2.rating)>=3 
AND  COUNT(DISTINCT t2.user) >= 3