我没有完全理解为什么排序与#34; ORDER BY平均值,投票DESC",因为我需要最高的平均值和投票从上到下,但是DESC没有解决什么问题。我在var_dump http://pastie.org/private/b05smuh0fvw72wwp2w1zq最高entrie的结果是在底部,但我需要从顶部开始并且到底部。
SELECT c.*, r.votes, c.total_comments,
ROUND(sumrate / votes) AS average
FROM catalog c LEFT JOIN
(SELECT r.object_id, COUNT(*) as votes, SUM(r.rate) as sumrate
FROM ratings r
GROUP BY r.object_id
) r
ON r.object_id = c.catalog_id LEFT JOIN
(SELECT c.catalog_id, COUNT(*) as total_comments
FROM comments c
GROUP BY c.catalog_id
) c
ON c.catalog_id = c.catalog_id
GROUP BY c.catalog_id
ORDER BY average, votes DESC;
答案 0 :(得分:5)
因为您可以定义每个列的顺序,而不仅仅是order by
。默认值为ASC
。
您的订单
ORDER BY average, votes DESC
自动转入
ORDER BY average ASC, votes DESC
但你正在寻找
ORDER BY average DESC, votes DESC
答案 1 :(得分:3)
您需要为这两列指定DESC。否则默认为ASC
SELECT c.*, r.votes, c.total_comments,
ROUND(sumrate / votes) AS average
FROM catalog c LEFT JOIN
(SELECT r.object_id, COUNT(*) as votes, SUM(r.rate) as sumrate
FROM ratings r
GROUP BY r.object_id
) r
ON r.object_id = c.catalog_id LEFT JOIN
(SELECT c.catalog_id, COUNT(*) as total_comments
FROM comments c
GROUP BY c.catalog_id
) c
ON c.catalog_id = c.catalog_id
GROUP BY c.catalog_id
ORDER BY average DESC, votes DESC;