情況:當創建數據庫時某個字段使用【varchar】或者【char】類型的,但是后續需求出現變化,需要對這個字段中的數據有個大小的排序,那么問題來了,字符串形式的數據如何進行類似【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);