SQL - ORDER BY not working properly?

前端 未结 4 1447
别跟我提以往
别跟我提以往 2020-12-11 16:15

SELECT test_column FROM test_table ORDER BY test_column gives me this:

1   
12  
123  
2  
3

Why not:

1
2
         


        
相关标签:
4条回答
  • 2020-12-11 16:30

    Try

    SELECT test_column 
    FROM test_table 
    ORDER BY cast(test_column as int)
    

    But you should look into changing the column types to the correct ones.

    0 讨论(0)
  • 2020-12-11 16:33

    This work me:-

    ORDER BY cast(test_column as SIGNED)
    
    0 讨论(0)
  • 2020-12-11 16:42

    Check if the type of the column is varchar or something similar. Looks like it is being ordered by string value, not by numeric value. If the column only contains numbers it should better be of type int.

    0 讨论(0)
  • 2020-12-11 16:44

    The sort is working. It's a lexicographic sort (alphabetical). It appears that that column has a text (char, varchar, ...) type, so the ordering you'll get is textual and not numeric.

    If you want a numerical sort, use a numeric column type (e.g. int). (Or cast the column appropriately.)

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