Natural Sort in MySQL

后端 未结 21 1086
南旧
南旧 2020-11-22 02:25

Is there an elegant way to have performant, natural sorting in a MySQL database?

For example if I have this data set:

  • Final Fantasy
  • Final Fant
21条回答
  •  孤独总比滥情好
    2020-11-22 03:00

    To order:
    0
    1
    2
    10
    23
    101
    205
    1000
    a
    aac
    b
    casdsadsa
    css

    Use this query:

    SELECT 
        column_name 
    FROM 
        table_name 
    ORDER BY
        column_name REGEXP '^\d*[^\da-z&\.\' \-\"\!\@\#\$\%\^\*\(\)\;\:\\,\?\/\~\`\|\_\-]' DESC, 
        column_name + 0, 
        column_name;
    

提交回复
热议问题