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.
ORDER BY doesn't like numbers.
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Mac
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
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