How to order 1,2,3 not 1, 10, 11, 12 in mySQL

前端 未结 6 1134
天涯浪人
天涯浪人 2020-12-29 02:22

The following code outputs in order of 1, 10, 11, 12 of id.

I want to make it 1,2,3,4...

Could anyone tell me what I should do please.

$Q = $         


        
6条回答
  •  有刺的猬
    2020-12-29 03:00

    As previously mentioned MySQL doesn't support alphanumeric sorting. One common trick to solve this is to first order by length:

    ORDER BY LENGTH(column_name), column_name
    

    As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc.

提交回复
热议问题