[MySQL] 把字串當作是數字排序

同樣是 1~20 的排序,字串的排序是這樣:

1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
23
24
25
26
27
28
29


在 MySQL 裡面,只要用 CAST 把字串轉為數字再排序即可:

SELECT * FROM  table_name ORDER BY CAST(field_name AS UNSIGNED) 

CAST 在 MySQL 的官方說明:
http://dev.mysql.com/doc/mysql/en/Cast_Functions.html

2 comments On [MySQL] 把字串當作是數字排序

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar