当前位置: 代码迷 >> 综合 >> Mysql把字符串类型转换为数字后,进行排序操作
  详细解决方案

Mysql把字符串类型转换为数字后,进行排序操作

热度:96   发布时间:2024-01-17 06:24:58.0

情景:最近项目中在写sql时,遇到要按字符串类型的数字排序,直接排序不行,那就需要把字符串类型转换成数字后,再进行排序。

三种解决方法:

1.数字后面直接加0,即:(value+0)格式

select * from sys_user order by (sort+0) desc 

2.使用cast()函数,即:cast(value as type)格式

select * from sys_user order by cast(sort as signed) desc 

3.使用convert()函数,即:convert(value , type)格式

select * from sys_user order by convert(sort , signed) desc 

备注:value 代表字符串类型的字段 ,type代表要转换的类型  signed为整数类型 

OK 今天就到这里啦

  相关解决方案