Natural Sort in MySQL

后端 未结 21 1488
南旧
南旧 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条回答
  •  猫巷女王i
    2020-11-22 03:09

    Here is a simple one if titles only have the version as a number:

    ORDER BY CAST(REGEXP_REPLACE(title, "[a-zA-Z]+", "") AS INT)';
    

    Otherwise you can use simple SQL if you use a pattern (this pattern uses a # before the version):

    create table titles(title);
    
    insert into titles (title) values 
    ('Final Fantasy'),
    ('Final Fantasy #03'),
    ('Final Fantasy #11'),
    ('Final Fantasy #10'),
    ('Final Fantasy #2'),
    ('Bond 007 ##2'),
    ('Final Fantasy #01'),
    ('Bond 007'),
    ('Final Fantasy #11}');
    
    select REGEXP_REPLACE(title, "#([0-9]+)", "\\1") as title from titles
    ORDER BY REGEXP_REPLACE(title, "#[0-9]+", ""),
    CAST(REGEXP_REPLACE(title, ".*#([0-9]+).*", "\\1") AS INT);     
    +-------------------+
    | title             |
    +-------------------+
    | Bond 007          |
    | Bond 007 #2       |
    | Final Fantasy     |
    | Final Fantasy 01  |
    | Final Fantasy 2   |
    | Final Fantasy 03  |
    | Final Fantasy 10  |
    | Final Fantasy 11  |
    | Final Fantasy 11} |
    +-------------------+
    8 rows in set, 2 warnings (0.001 sec)
    

    You can use other patterns if needed. For example if you have a movie "I'm #1" and "I'm #1 part 2" then maybe wrap the version e.g. "Final Fantasy {11}"

提交回复
热议问题