Postgresql sorting mixed alphanumeric data

前端 未结 7 1457
无人及你
无人及你 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:37

    Tor's last SQL worked for me. However if you are calling this code from php you need add extra slashes.

    SELECT name
    FROM folders
    ORDER BY
    COALESCE(SUBSTRING(name FROM '^(\\\\d+)')::INTEGER, 99999999),
    SUBSTRING(name FROM '^\\\\d* *(.*?)( \\\\d+)?$'),
    COALESCE(SUBSTRING(name FROM ' (\\\\d+)$')::INTEGER, 0),
    name;
    
    0 讨论(0)
提交回复
热议问题