Varchar to number conversion for sorting

前端 未结 5 854
礼貌的吻别
礼貌的吻别 2020-12-05 07:23

I have a query ordered by column:

select * from mytable order by column asc — sort table

column type is varchar,

5条回答
  •  执笔经年
    2020-12-05 07:49

    You can use this if you want to treat column as INT only:

    SELECT * FROM mytable ORDER BY column+0;
    1
    10
    11
    12
    13
    100
    

    Or this if you want to treat column as both INT and VARCHAR

    SELECT * FROM mytable ORDER BY column+0, column; #this will sort the column by VARCHAR first and then sort it by INT
    
    abc
    xyz
    1
    10
    11
    12
    13
    100
    

提交回复
热议问题