Page 1 of 1

ORDER BY doesn't like numbers.

Posted: Sat Nov 09, 2002 4:50 pm
by Gen-ik
I have a MySQL database table with one of the colums names ID.

I do a mysql_query and ORDER BY id.

I'm not getting the expected result as mysql seems to be treating the numbers as strings.

I get something like this..

1, 10, 11, 2, 3, 31, 33.. and so on.

Is there any thing I can do about this?


Thanks for your time.

Posted: Sat Nov 09, 2002 8:22 pm
by volka
if id is kind of a numerical value (tiny/medium/large int, ....) mysql uses the correct natural order.

Posted: Mon Nov 11, 2002 2:14 am
by twigletmac
Yup, if you're storing your number in a varchar column (ie. a text column intended for strings) instead of one intended for integers then MySQL will treat the numbers as strings. If however, you are storing the numbers in a tiny/small/medium/large int column then you've got another prob somewhere.

Mac

Posted: Tue Nov 12, 2002 9:50 am
by Rob the R
I'm sure Volka and Mac are right, but if the column type has to remain a varchar, you can try using the CAST function in the ORDER BY clause:
SELECT id FROM table ORDER BY CAST(id as SIGNED)

I don't have MySQL in front of me, so I can't confirm if this will work or not, but I can't find any evidence it wouldn't :wink: