Oracle-Mysql中對字串排序
阿新 • • 發佈:2018-12-09
mysql中對字串排序,字串中有數字有漢字,想按數字的大小來進行排序。僅僅用order by排序,效果不是想要的。
sql語句為:
- select id,dict_name,type_code from t_dictionary where type_code='GRADE' ORDER BY `dict_name`;
排序效果如下:
因為字串排序是先比較字串第一個字元的大小。而不是像int型比較整個數字的大小。要想用字串資料排成整型排序的效果,可以採用如下三種方式:
- 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);