MySQL order by string with numbers

后端 未结 7 866
谎友^
谎友^ 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:32

    I couldn't get this working for my issue which was sorting MLS Numbers like below:

    V12345 V1000000 V92832

    The problem was V1000000 wasn't being valued higher than the rest even though it's bigger.

    Using this solved my problem:

    ORDER BY CAST(SUBSTR(col_name FROM 2) AS UNSIGNED) DESC
    

    Just removed the SUBSTR(col_name FROM 1 FOR 1)

    0 讨论(0)
提交回复
热议问题