mysql查询,根据投票选择最高评论

时间:2011-02-21 00:58:47

标签: mysql

我有2张表comments | votes

the `votes` structure 
[`id` | `user_id` | `comment_id`  | `rating`]

并且评论将comment_id作为主要的确定? 现在我想根据评级总和获得最高评价

[评分为0或1]

我也希望得到顶级用户

1 个答案:

答案 0 :(得分:2)

热门评论

这假设注释表有一个名为comments_id的列 SELECT A. * FROM comments INNER JOIN(SELECT comment_id,SUM(rating)汇总FROM投票GROUP BY comment_id)B USING(comment_id)ORDER BY B.sumrating;

这假设注释表有一个名为id
的列 SELECT A. * FROM comments INNER JOIN(SELECT comment_id,SUM(rating)汇总FROM投票GROUP BY comment_id)B ON A.id = B.comment_id ORDER BY B.sumrating;

热门用户

这假设users表有一个名为user_id的列 SELECT A. * FROM users INNER JOIN(SELECT user_id,SUM(rating)汇总FROM投票GROUP BY user_id)B USING(user_id)ORDER BY B.sumrating;

这假设users表有一个名为id的列 SELECT A. * FROM users INNER JOIN(SELECT user_id,SUM(rating)汇总FROM投票GROUP BY user_id)B ON A.id = B.user_id ORDER BY B.sumrating;

热门用户和评论

这假设注释表有一个名为comments_id的列,而用户有一个名为user_id的列 SELECT B. *,C。* FROM (SELECT comment_id,user_id,SUM(rating)汇总FROM投票GROUP BY comment_id,user_id)A 评论B,用户C, 在哪里A.commentment_id = B.comment_id AND A.user_id = C.user_id ORDER BY A.sumrating,C.user_id,B.comment_id;

试一试!!!