Page 1 of 1

[SOLVED] Ordering problem

Posted: Thu Aug 11, 2005 4:19 pm
by sk8erh4x0r
I'm using this query:

SELECT * FROM `tutorials` ORDER BY `chars` DESC

the chars field holds a number value

The problem i'm having is that it's not ordering the way it should because there are high values below the lower values like so:

5435
2456
11254

Why isn't 11254 at the top?

Posted: Thu Aug 11, 2005 4:36 pm
by neophyte
What kind of field is chars?

Posted: Thu Aug 11, 2005 4:37 pm
by nielsene
what type is "chars" it looks like its a character string, so its sorting "alphabetically" not numerically. You might be able to cast it to an integer to make it sort properly. In PostGreSQL I'd try

Code: Select all

SELECT * FROM tutorials ORDER BY chars::INT DESC;
but I don't know the syntax fo MySQL.

Posted: Fri Aug 12, 2005 5:19 am
by feyd
MySQL ghetto quickie:

Code: Select all

SELECT * FROM tutorials ORDER BY chars+0 DESC;