Natural Sort in MySQL

后端 未结 21 1062
南旧
南旧 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:02

    I have tried several solutions but the actually it is very simple:

    SELECT test_column FROM test_table ORDER BY LENGTH(test_column) DESC, test_column DESC
    
    /* 
    Result 
    --------
    value_1
    value_2
    value_3
    value_4
    value_5
    value_6
    value_7
    value_8
    value_9
    value_10
    value_11
    value_12
    value_13
    value_14
    value_15
    ...
    */
    

提交回复
热议问题