1.问题描述
表中,字段:chapter_number 为varchar,存的值为1,2,3,4,5,10,11,12数字,进行按正序排序时,返回的数据为1,10,11,12,2,3,4,5
2.解决方法
your_column :你的字段名
cast(your_column as signed);
举例:
your_table: 你的表名
your_column:你的字段名
select * from your_table order by cast(your_column as signed);
3.cast方法说明
mysql cast()函数用于将值从一种数据类型转换为另一种特定数据类型。 cast()函数接受两个参数,即要转换的值和需要将该值转换为的数据类型。
可以转换给定值的数据类型为:
date:用于将值转换为date数据类型。返回的格式为“yyyy-mm-dd”。
datetime:用于将值转换为datetime数据类型。返回的格式为“ yyyy-mm-dd hh:mm:ss”。
time:用于将值转换为time数据类型。返回的格式为“hh:mm:ss”。
char:用于将值转换为char数据类型。
signed:用于将值转换为signed数据类型。
unsigned:用于将值转换为unsigned数据类型。
binary:用于将值转换为binary数据类型。
用法:
cast(input_value as datatype)
使用的参数:
input_value –用于指定需要转换的值。
datatype –它用于指定需要在其中转换值的数据类型。
返回值:
转换后,mysql cast()函数返回所需数据类型的值。
受支持的mysql版本:
mysql 5.7
mysql 5.6
mysql 5.5
mysql 5.1
mysql 5.0
mysql 4.1
mysql 4.0
mysql 3.23
示例1:实现cast()函数以将值转换为date数据类型。
select cast("2019-11-21" as date); 输出:2019-11-21
示例2:实现cast()函数以将值转换为char数据类型。
select cast(121 as char); 输出:121
示例3:实现cast()函数以将值转换为signed数据类型。
select cast(2-4 as signed); 输出:-2
示例4:实现cast()函数以将值转换为unsigned数据类型。
select cast(2-4 as unsigned); 输出:18446744073709551614
以上就是mysql对varchar类型数字进行排序的实现方法的详细内容,更多关于mysql varchar数字排序的资料请关注代码网其它相关文章!
发表评论