Skip to content


【转】MySql varchar排序,加減 CAST ,CONVERT

假如表结构如下
id(int) a(varchar)
1 1
2 2
3 10
4 20
5 3
6 30

第一种,cast
用法:cast(字段 as 数据类型) [当然是否可以成功转换,还要看数据类型强制转化时注意的问题]
实例:select cast(a as unsigned) as b from cardserver where order by b desc;

第二种,convert
用法:convert(字段,数据类型)
实例:select convert(a ,unsigned) as b from cardserver where order by b desc;

第三种,加減0法
用法:字段加0或减0操作 [在MYSQL中,varchar加减数字就会自动转换为数字操作]
实例:select a+0 as b from cardserver where order by b desc;

参考:http://hi.baidu.com/loglab/blog/item/37606cdd7744523f5982dd35.html

Posted in Mysql, 技术.

Tagged with .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.