热门评论,热门用户,热门用户和评论mysql

时间:2011-02-21 09:57:49

标签: mysql

我有两张桌子comments | votes

The `votes` structure is:
[`id` | `user_id` | `comment_id`  | `rating`]

并且评论将comment_id作为主要内容。 现在我想根据评级总和获得最高评价。

[评分为0或1]

我也希望得到顶级用户。

1 个答案:

答案 0 :(得分:1)

这将返回前10条评论更改或更少的限制。 显然,将*替换为您希望从评论中返回的列

select *
from comments x
join (select comment_id, sum(rating)
      from votes 
      group by comment_id
      order by sum(rating) desc
      limit 10 ) z on x.comment_id = z.comment_id

用户将以相同的方式完成,只需从user_id更改comment_id并加入您

相关问题