How to ORDER BY varchar field as number?

前端 未结 4 1080
春和景丽
春和景丽 2020-12-28 07:52

I have problem with sort/order by, not working like I need.

SELECT `proc` FROM `table` ORDER BY `proc` DESC;

Result:

80.0 p         


        
4条回答
  •  囚心锁ツ
    2020-12-28 08:55

    It looks like "proc" is a string (varchar field), so it gets ordered lexically. If it is so, you can probably order it by

    SELECT `proc` FROM `table` ORDER BY convert(`proc`, decimal) DESC;
    

    Please note that such queries will be very slow, and for any serious usage it's better to use numeric columns for storing numeric data.

提交回复
热议问题