zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql—将字符型数字转成数值型数字

mysql字符 数字 数值
2023-09-11 14:19:36 时间

今天写sql语句时,相对字符串类型的数字进行排序,怎么做呢?

需要先转换成数字再进行排序

1.直接用加法

  字符串+0

eg:

  select * from orders order by (mark+0)  desc

2.使用函数

CAST(value as type);

CONVERT(value, type);

注:

  这里的type可以为:

    浮点数 : DECIMAL 
    整数 : SIGNED
    无符号整数 : UNSIGNED 

eg:

  select * from orders order by CONVERT(mark,SIGNED)  desc

  select * from orders order by CAST(mark as SIGNED)  desc