MySQL 'Order By' - sorting alphanumeric correctly

后端 未结 15 2138
执念已碎
执念已碎 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:55

    If you need to sort an alpha-numeric column that does not have any standard format whatsoever

    SELECT * FROM table ORDER BY (name = '0') DESC, (name+0 > 0) DESC, name+0 ASC, name ASC
    

    You can adapt this solution to include support for non-alphanumeric characters if desired using additional logic.

提交回复
热议问题