1. 程式人生 > >MySQL型別轉換 使用CAST將varchar轉換成int型別排序

MySQL型別轉換 使用CAST將varchar轉換成int型別排序

mysql為我們提供了兩個型別轉換函式:CAST和CONVERT

BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL
SIGNED [INTEGER]
TIME
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