SQL排序不就是order by 怎麼還會有數字排序?
當然就是有問題,還真是怪呢!廢話不多說,看個範例
假設有一database 有一欄數字的欄位number(型態:varchar),1,-2,2,10然後對這欄的數字作排序
SQL語法:
select mumber from table order by mumber
結果:
1
10
-2
2
怎麼變成這樣,不是我要的答案啦!所以就要用cast啦!
SQL語法:
select mumber from table order by cast(mumber as unsigned)
結果:
1
-2
2
10
呃~還是不一樣!因為unsigned是不包含負號下去排序,所以改成signed
SQL語法:
select mumber from table order by cast(mumber as signed)
結果:
-2
1
2
10