【发布时间】:2014-09-22 15:36:31
【问题描述】:
我目前在 mysql 中有 2 个表; cmets 和 comment_rating
comment_rating 具有以下结构:
+------------+
| Field |
+------------+
| id |
| comment_id |
| user_id |
| positive |
| created_at |
+------------+
positive 字段为 1 或 -1 1 表示肯定(赞成),-1 表示否定(反对)
我有这个当前的查询,这将使我获得最高评价:
SELECT *, COUNT(comment_rating.id) AS rating_count FROM comments LEFT JOIN comment_rating ON comments.id = comment_rating.comment_id GROUP BY comments.id ORDER BY rating_count DESC
我需要知道如何(使用 mysql 查询)我能够获得按最佳评级排序的 cmets; 含义按每条评论的评分总和排序。
例子:
- 评论 X 有 2 个赞成票和 4 个反对票(总共 -2 个)
- 评论 Y 无票(共 0 票)
- 评论 Z 有 1 个赞(总共 1 个)
这些出来的顺序是:
- 评论Z
- 评论Y
- 评论 X
【问题讨论】:
-
使用
SUM而不是COUNT。