在SQL(Collaborative Filtering)中返回与另一个用户共同的行

时间:2016-04-25 02:39:25

标签: mysql sql collaborative-filtering

我正在尝试使用MySQL构建一个基本的协同过滤推荐系统。我有一个这样的用户评级表:

    user_id movie_id rating
1   131     342      3      <<< User 131 has rated movie 342 
2   131     312      5      <<< and also 312
3   122     312      4  
4   213     342      5
5   141     342      5      <<< User 141 has rated 342
6   141     312      4      <<< and also 312 (2 movies in common) 
7   141     323      3

所以我试图找到类似的用户131.我想返回至少有两部电影并且评分高于3的用户。因此它应该返回第5行和第6行(如上所示)。

这是我到目前为止所做的:

SELECT * from user_ratings
WHERE rating >= 3
AND movie_id IN (SELECT movie_id from user_rating WHERE user_id = 131)
AND user_id != 131 

返回:

    user_id movie_id rating
3   122     312      4     <<< Don't want these two
4   213     342      5     <<<
5   141     342      5     
6   141     312      4      

它返回用户与131共有的电影,但我需要它才能显示至少有两个共同项目的用户。我怎么能这样做?我不确定如何继续:(

2 个答案:

答案 0 :(得分:1)

您可以先找到user_id的电影号码user_id = 131 rating > 3 IN WHERE。然后在SELECT * FROM user_ratings WHERE user_id IN( SELECT user_id FROM user_ratings WHERE movie_id IN (SELECT movie_id FROM user_ratings WHERE user_id = 131) AND rating > 3 GROUP BY user_id HAVING COUNT(*) >= (SELECT COUNT(*) FROM user_ratings WHERE user_id = 131) ) AND rating > 3 子句中使用export function fetchVerificationLink() { return async( dispatch ) => { dispatch(requestVerificationLink()); let response, link; try { response = await read(urls.etradeVerification); // I have a wrapper around node-fetch, but failed to handle the promise that fetch returns dispatch(receiveVerificationLink(response)); } catch ( error ) { console.error('Unable to get verification link from eTrade.', error); } } } 来获取其他数据:

SQL Fiddle

export function fetchVerificationLink() {
    return async( dispatch ) => {
        dispatch(requestVerificationLink());

        let response, link;
        try {
            response = await read(urls.etradeVerification);
            try {
                link = await response.json();  // Here was the solution to my problem.
                dispatch(receiveVerificationLink(link));
            }
            catch ( error ) {
                console.error('Unable to parse authorization link response.', error);
            }                
        }
        catch ( error ) {
            console.error('Unable to get verification link from eTrade.', error);
        }
    }
}

答案 1 :(得分:0)

您可以使用movie_id上的自我加入来获取与用户131评分为至少2个相同电影的user_ids列表,评级为4或更高:

select ur2.user_id
from user_ratings ur1
join user_ratings ur2 
    on ur2.movie_id = ur1.movie_id
    and ur2.user_id <> ur1.user_id
where ur1.user_id = 131
    and ur2.rating > 3
group by ur2.user_id
having count(*) >= 2

http://sqlfiddle.com/#!9/06b56/4