给定用户,选择具有最常见评级的用户

时间:2012-03-14 19:20:18

标签: mysql sql

说我有一张评分表:

create table ratings (
    user_id int unsigned not null,
    post_id int unsigned not null,
    rating set('like', 'dislike') not null,
    primary key (user_id, post_id)
);

对于id 1的给定用户,如何选择具有更多喜欢的用户?和不喜欢的用户有什么不同?而且有更多评级(喜欢还是不喜欢)的用户?我猜这些查询会非常相似,但是我无法想出任何一个问题。我将根据我的进展进行更新。

感谢任何帮助,谢谢!

4 个答案:

答案 0 :(得分:1)

尝试一下:

select r2.user_id from (
  select post_id, rating from ratings,
    (select @userId := 2) init
  where user_id = @userId
) as r1
join ratings r2
on r1.post_id = r2.post_id and r1.rating = r2.rating
where r2.user_id != @userId and r2.rating = 'like'
group by r2.user_id
order by count(*) desc
limit 1

通过更改字符串,它应该适用于喜欢和不喜欢。并且要更改用户只需修改变量赋值。

以下内容应该适用于不喜欢和喜欢的共同点(只需删除过滤条件):

select r2.user_id from (
  select post_id, rating from ratings,
    (select @userId := 2) init
  where user_id = @userId
) as r1
join ratings r2
on r1.post_id = r2.post_id and r1.rating = r2.rating
where r2.user_id != @userId
group by r2.user_id
order by count(*) desc
limit 1

答案 1 :(得分:1)

select
    r1.user_id as user1
    ,r2.user_id as user2
    ,r1.rating as rating
    ,count(*) as num_matching_ratings
from
    ratings r1 
    inner join ratings r2
        on r1.post_id = r2.post_id 
            and r1.rating = r2.rating
            and r1.user_id <> r2.user_id --don't want to count
                                         --matches with self
where
    r1.user_id = 1 -- change this to any user, or use a
                   -- variable to increase reusebility
    and r1.rating = 'like' -- set this to dislike to common dislikes
group by
    r1.user_id
    ,r2.user_id
    ,r1.rating
having
    count(*) > 1 --show only those with more than 1 in common
order by
    count(*) desc
/* limit 1 -- uncomment to show just the top match */

通过将表格连接在一起,我们可以计算第二个用户对文章进行类似评分的出现次数。此查询将从最常见的评估返回到最少的评估。如果取消注释“限制1”语句,它将仅返回最常见的匹配。

答案 2 :(得分:0)

请原谅我的语法,我不经常写原始的sql。你可以考虑这个psudocode。

首先,我得到id为1的表

view1 = SELECT * FROM ratings, WHERE ( user_id = 1)

然后我加入评级

view2 = select * from view1, ratings, where(view1.rating = ratings.rating AND view1.post_id = records.post_id)

然后我按计数汇总

view3 = select count from view2 group by (user_id)

然后我会得到最大值。

现在,这只是对我的初步想法的算法概述。我认为它不会特别有效,你可能不会使用那种语法。

答案 3 :(得分:0)

基于Chris和Mostacho的答案,我做了以下查询。我不是百分之百确定它每次都有效,但我还没有发现它的缺陷。

select r2.user_id
from ratings r1
join ratings r2
on r1.user_id <> r2.user_id
and r1.post_id = r2.post_id 
and r1.rating = r2.rating
where r1.user_id = 1 
and r1.rating = 'like'
group by r2.user_id
order by count(r2.user_id) desc
limit 1

此查询返回用户ID更常见的用户ID。要获取具有更多常用评级的用户,只需从where子句中删除and r1.rating = 'like'