Sort numbers (and numbers+letters) with sqlite

ぐ巨炮叔叔 提交于 2019-12-10 06:43:02

问题


I'm trying to sort a VarChar field with Sqlite.

The field can contain numbers or numbers+letters, but I need to sort in numerical order, like this:

1
1a
1b
5
5x
5y
10
10d
10e
10g1
11
11a
11b
100c
100f

Any ideas? I've been able to do this... it is close (but not quite) what I need:

Pad the start of the field with '00000', and then sort on the 1st five letters


回答1:


Easy

select col from tbl order by col*1, col



回答2:


There's no easy way to do this with the built-in functions. Use sqlite3_create_collation (or the equivalent wrapper in your preferred programming language) to define a string comparison function that implements natural sort.



来源:https://stackoverflow.com/questions/5189511/sort-numbers-and-numbersletters-with-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!