ORDER BY problem
Moderator: General Moderators
ORDER BY problem
When I use the query:
SELECT * FROM ELEC ORDER BY id DESC
And my id in table:
10, 11, 12, 13, 3, 5, 6, 7, 8, 9
But the result is not correct (or I am doing something wrong):
9, 8, 7, 6, 5, 3, 13, 12, 11, 10
How can I make it output:
13 . . . 3
Please help,
Foxy
SELECT * FROM ELEC ORDER BY id DESC
And my id in table:
10, 11, 12, 13, 3, 5, 6, 7, 8, 9
But the result is not correct (or I am doing something wrong):
9, 8, 7, 6, 5, 3, 13, 12, 11, 10
How can I make it output:
13 . . . 3
Please help,
Foxy
Re: ORDER BY problem
Please, post your table structure (i.e. DESCRIBE TABLE my_table),
post your exact query,
post the exact result produced by this query.
post your exact query,
post the exact result produced by this query.
There are 10 types of people in this world, those who understand binary and those who don't
Re: ORDER BY problem
Your id field is obviously ASCII text, not a number. Usually not a good idea for an ID field. In any case, it is doing exactly what you asked it to do, sort descending. Since it is ASCII text, the highest entry is "9", then "8", and so on until it gets to a text beginning with "1", the highest being "13". That's how ASCII text is sorted, descending. If you expect it to be the same as if they were numbers you could convert the id field to integers, but that would give you problems if you ever have any non-numeric values in your field.
Re: ORDER BY problem
Thanks, that's what I thought I had to docalifdon wrote:Your id field is obviously ASCII text, not a number. Usually not a good idea for an ID field. In any case, it is doing exactly what you asked it to do, sort descending. Since it is ASCII text, the highest entry is "9", then "8", and so on until it gets to a text beginning with "1", the highest being "13". That's how ASCII text is sorted, descending. If you expect it to be the same as if they were numbers you could convert the id field to integers, but that would give you problems if you ever have any non-numeric values in your field.
Re: ORDER BY problem
Nice catchcalifdon wrote:Your id field is obviously ASCII text, not a number.
There are 10 types of people in this world, those who understand binary and those who don't
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: ORDER BY problem
You'll notice it the next time, though. :3VladSun wrote:Nice catchI feel a little bit stupid now
Re: ORDER BY problem
Don't feel bad, Vlad, we all need to feel stupid now and then, it's good for the soul! If I don't feel stupid at least a few times a week, I try to do something so I will! 
Re: ORDER BY problem
Well... obviously (for me) I prefer the "linear iteration" model of debugging someone's else issues - i.e. the KISS principle applied.superdezign wrote:You'll notice it the next time, though. :3VladSun wrote:Nice catchI feel a little bit stupid now
From my experience, it works better than "supposing" more "complicated" issues.
But in this particular case I really, really should have noticed the "issue pattern"
And yes - next time I won't miss it, ha-ha-ha
Last edited by VladSun on Mon Jul 27, 2009 5:37 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Re: ORDER BY problem
I second thatcalifdon wrote:Don't feel bad, Vlad, we all need to feel stupid now and then, it's good for the soul! If I don't feel stupid at least a few times a week, I try to do something so I will!
There are 10 types of people in this world, those who understand binary and those who don't