mysql中对字符串排序,字符串中有数字有汉字,想按数字的大小来进行排序。仅仅用order by排序,效果不是想要的。mysql
sql语句为:sql
- select id,dict_name,type_code from t_dictionary where type_code='GRADE' ORDER BY `dict_name`;
排序效果以下:spa
由于字符串排序是先比较字符串第一个字符的大小。而不是像int型比较整个数字的大小。要想用字符串数据排成整型排序的效果,能够采用以下三种方式:.net
- select id,dict_name,type_code from t_dictionary where type_code='GRADE' ORDER BY `dict_name`*1;
- select id,dict_name,type_code from t_dictionary where type_code='GRADE' ORDER BY dict_name+0;
- select id,dict_name,type_code from t_dictionary where type_code='GRADE' ORDER BY CAST(dict_name AS DECIMAL);