[譯]對於SQL中的varchar字段按照int進行排序

原文鏈接: https://stackoverflow.com/questions/11808573/sql-order-string-as-number

對某列數據進行排序的時候, 我希望他按照以下順序進行排序

1 2 3 4 5 6 7 8 9 10...

不過現在的順序如下:

1 10 2 3 4 5 6 7 8 9

解決方案:
將特定列名顯式轉換成int類型

select col from yourtable
order by cast(col as unsigned)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章