How to ORDER BY varchar field as number?

前端 未结 4 1081
春和景丽
春和景丽 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:36

    The column field for proc is a VARCHAR or CHAR and it's treating it as a literal string--sorting alphabetically.

    Convert the column to double or float or cast the value

    SELECT `proc` FROM `table` ORDER BY CAST(`proc` AS decimal) DESC;
    
    0 讨论(0)
  • 2020-12-28 08:36

    If you always have the same number of decimal points, my approach would be:

    SELECT `proc` FROM `table` ORDER BY LENGTH(proc) DESC, proc DESC;
    
    0 讨论(0)
  • 2020-12-28 08:46

    There's something fundamentally wrong with your table design. Instead of using values like '80.0 proc' in a VARCHAR column, you should just keep 80.0 in a column of type REAL (or any suitable numerical type that's appropriate for your data). You could do dynamic conversion, only to be used in the ORDER BY expression, but this is also likely to deteriorate the performance of your query.

    Adding "proc" to your text here doesn't seem useful, and it will also prevent you from doing a simple conversion.

    Surprisingly (see che's answer), apparently, convert(..., decimal) is capable of ignoring the trailing rubbish. It's not something you should rely on in general, though.

    The documentation on this aspect of the conversion isn't particularly clear. It's worth reading that section to be aware of the limitations of string/numbers (which would happen in general), for example:

    mysql> SELECT '18015376320243459' = 18015376320243459;
            -> 0
    

    If that behaviour changed, you could probably use replace() in this case, just to get rid of ' proc'.

    For something more complex, you could potentially use a regular expression replacement to extract the numerical value from your string and cast it into a number before sorting, but this is not supported out of the box in MySQL and that would be rather clunky anyway (fix the problem at its source: your column data type).

    To deal with your legacy data, you could add an extra column and use an external program (in any language that's capable of doing a regexp replace), which shouldn't be too difficult.

    0 讨论(0)
  • 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.

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