MySQL 'Order By' - sorting alphanumeric correctly

后端 未结 15 2298
执念已碎
执念已碎 2020-11-22 12:02

I want to sort the following data items in the order they are presented below (numbers 1-12):

1
2
3
4
5
6
7
8
9
10
11
12

However, my query - using

15条回答
  •  执念已碎
    2020-11-22 12:57

    MySQL ORDER BY Sorting alphanumeric on correct order

    example:

    SELECT `alphanumericCol` FROM `tableName` ORDER BY 
      SUBSTR(`alphanumericCol` FROM 1 FOR 1), 
      LPAD(lower(`alphanumericCol`), 10,0) ASC
    

    output:

    1
    2
    11
    21
    100
    101
    102
    104
    S-104A
    S-105
    S-107
    S-111
    

提交回复
热议问题