MySQL ORDER BY十进制值不起作用

时间:2017-03-01 08:41:25

标签: mysql sql sql-order-by

带有order by子句的SQL查询无法正常工作。

VALUES:
5.0000   
4.0000   
4.5000  
4.7500  
4.0000  
5.0000

查询:

SELECT  AVG(rating) as rating
FROM review
GROUP BY id
ORDER BY CAST(`rating` as signed) DESC  

执行此查询时,输出为:

5.0000  
4.7500  
4.5000  
5.0000  
4.0000  
4.0000

我的评分栏是int(10)

1 个答案:

答案 0 :(得分:3)

您必须按AVG(rating)订购,而不是rating

SELECT AVG(rating) as rating FROM review GROUP BY id ORDER BY AVG(rating) DESC 

在您的情况下,别名和列具有相同的名称。为了避免混淆,你可以这样做:

SELECT AVG(rating) as avgrating FROM review GROUP BY id ORDER BY avgrating DESC