MySQL order by string with numbers

后端 未结 7 884
谎友^
谎友^ 2020-12-11 01:57

I have strings such as M1 M3 M4 M14 M30 M40 etc (really any int 2-3 digits after a letter) When I do \" ORDER BY name \" this returns:

M1, M14, M3

7条回答
  •  难免孤独
    2020-12-11 02:17

    It split number and letters as separately.

    SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(
    SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(col,'1', 1), '2', 1), '3', 1), '4', 1), '5', 1), '6', 1)
    , '7', 1), '8', 1), '9', 1), '0', 1) as new_col  
    FROM table group by new_col; 
    

提交回复
热议问题