MySQL类型转换 使用CAST将varchar转换成int类型排序

mysql为我们提供了两个类型转换函数:CAST和CONVERT,现成的东西我们怎能放过?

1 BINARY[(N)]
2 CHAR[(N)]
3 DATE
4 DATETIME
5 DECIMAL
6 SIGNED [INTEGER]
7 TIME
8 UNSIGNED [INTEGER]

例子:

--使用CAST将varchar转换成int类型排序
select server_id from cardserver where game_id = 1 order by CAST(server_id as SIGNED) desc limit 10;

--使用CONVERT将varchar转换成int类型排序
select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED) desc limit 10

你可能感兴趣的:(mybatis,mysql)