MySQL无法正确排序数据 [英] MySQL not Sorting Data Correctly

查看:147
本文介绍了MySQL无法正确排序数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MySQL很长时间了,而且从未遇到过这个问题.我有一个表,用于存储应用程序的分数.出于某种原因,当我按score ASC进行排序时,将首先显示最高得分,而最低得分最后显示.请查看下面的屏幕截图:

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:

这是我的查询:

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?

推荐答案

将score的数据类型从字符串(例如varchar/text)更改为数字(例如int).那应该可以解决排序问题.

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

当值以字符串形式(按字母顺序)排序时,"60"中的"6"位于"8"之前.

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

作为临时解决方法,您还可以尝试order by score+0 asc尝试将您的值转换为数字.

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

这篇关于MySQL无法正确排序数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆