Postgresql sorting mixed alphanumeric data

前端 未结 7 1512
无人及你
无人及你 2020-12-30 05:32

Running this query:

select name from folders order by name

returns these results:

alphanumeric
a test
test 20
test 19
test          


        
7条回答
  •  滥情空心
    2020-12-30 06:22

    A Vlk's answer above helped me a lot, but it sorted items only by the numeric part, which in my case came second. My data was like (desk 1, desk 2, desk 3 ...) a string part, a space and a numeric part. The syntax in A Vlk's answer returned the data sorted by the number, and at that it was the only answer from the above that did the trick. However when the string part was different, (eg desk 3, desk 4, table 1, desk 5...) table 1 would get first from desk 2. I fixed this using the syntax below:

        ...order by SUBSTRING(name,'\\w+'), SUBSTRINGname FROM '([0-9]+)')::BIGINT ASC;
    

提交回复
热议问题