解决mysql字符串类型的数字排序出错:cast(year as signed)
作者:孤独王者¥
这篇文章主要介绍了解决mysql字符串类型的数字排序出错问题 :cast(year as signed),如有错误或未考虑完全的地方,望不吝赐教
mysql字符串类型的数字排序出错:cast(year as signed)
将字符串类型的数字转化为数字类型的数字就可以了,如下:
select year, month from table order by cast(year as signed), cast(month as signed) asc
SQL中CAST()函数的用法
简单介绍
cast()函数: 强制数据类型转换
cast()函数用法: cast(字段名 as 转换的类型)
实际应用
例:
表名:table 字段名:data (data 为 2019-09-16 18:53:00)
1. 转换为整型(int)
select cast (data as signed) as data from table;
查询结果为:
20190916185300
2. 转换为字符型(char)
select cast(data as char) as data from table;
查询结果为:
2019-09-16 18:53:00
3. 转换为日期时间型(datetime)
select cast(data as datetime) as data from table;
查询结果为:
2019-09-16 18:53:00
5. 转换为日期型(date)
select cast(data as date) as data from table;
查询结果为:
2019-09-16
7. 转换为时间型(time)
select cast(data as time) as data from table;
查询结果为:
18:53:00
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。