知识点:
整数 : SIGNED
无符号整数 : UNSIGNED
情景:码值因为开发需要存成了varchar类型,但是又需要根据码值做个排序列。
表结构:
由于code存成了varchar类型,导致在给code值排序的时候,出现了乱序的情况。但是直接cast(code_type as int) 行不通。
select code_type , code from t_demand_code_01 tdc where code_type ='project_group' order by code ;
解决办法:
1、利用cast(varchar as unsigned),将varchar类型转为int类型
select code_type , code from t_demand_code_01 tdc where code_type ='project_group' order by cast(code as unsigned);
2、利用cast(varchar as signed),将varchar类型转为int类型
select code_type , code from t_demand_code_01 tdc where code_type ='project_group' order by cast(code as signed);