MySQL is sorting a numeric column as alpha

I am sorting a multi-table join on sort columns from each but it is sorting the numeric columns as if they are alpha columns, i.e. 11 sorts before 2. How do I get around this and why does it do it in the first place?

If 11 is ordered before 2 then the expression in your order by clause must be of character type (assuming that you have an ascending sort direction).

You can force it to be numeric by using an explicit cast operation or implicitly by adding a numeric value to expression, e.g.

order by <your expression> + 0

implicit hacks, gotta love 'em :slight_smile:

I would go with CAST, but that’s me.

My bad. I was mistaken. The query I was playing with was actually using a character type sort. Sorry. :hushed:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.