MySQL 字符型数据按数值型排序问题
天蓬老师
天蓬老师 2017-04-17 13:02:30
0
3
441

字段为字符型,数据格式为 字符+数字
例如
dslr831
dslr832
dslr833

用什么方法才能按照数值型排序,试过CAST, 以及ABS都不能够完美实现

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
Peter_Zhu

If the character type length and prefix are the same,

dslr831
dslr832
dslr833

prefix dslr, the real number length is 3 digits, so sorting by characters should be enough.

Otherwise, if the amount of data is not large, just take it out and sort it in memory. Otherwise, it is recommended to use a custom function
Cut the fields out first, perhaps into a view, and then sort the view.

Follow the tips of another answer:

SELECT CONVERT(SUBSTRING(dslr, 5), INTEGER) AS dslr from table order by dslr;

Reference:
http://stackoverflow.com/questions/5960620/convert-text-into-number-in...

迷茫

If it really can’t be solved, it is recommended to handle it on the PHP side (get other languages)

大家讲道理

SELECT SUBSTRING(dslr, 5) AS dslr from table order by --dslr;

table is the table name
5 You can change it at your own discretion
dslr is the field name

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!