MySQL not Sorting Data Correctly

ぐ巨炮叔叔 提交于 2020-01-11 12:05:07

问题


I have been using MySQL for a long time and I have never run across this issue. I have a table that stores the scores for an application. For some reason, when I sort by score ASC, the highest score is shown first with the lowest score being last. Please see the screenshot below:

Here is my query:

SELECT category, subject, max(score) as score FROM scores 
WHERE customer_id = 1086 AND category = 'Business' 
GROUP BY subject ORDER BY score ASC

Any thoughts on why this is happening?


回答1:


Change the datatype of score from string (eg varchar/text) to a number (eg int). That should solve the sorting issue.

When the values are sorted on string basis (alphabetically), the '6' in '60' comes before '8'.

As a temporary work-around you can also try order by score+0 asc to try and convert your value into a number.




回答2:


Try selecting CAST(max(score) AS INTEGER) as Score and then ORDER BY Score.



来源:https://stackoverflow.com/questions/21448165/mysql-not-sorting-data-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!