Page 1 of 1
ORDER BY problem
Posted: Mon Jul 27, 2009 3:47 pm
by Foxy999
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
Re: ORDER BY problem
Posted: Mon Jul 27, 2009 4:44 pm
by VladSun
Please, post your table structure (i.e. DESCRIBE TABLE my_table),
post your exact query,
post the exact result produced by this query.
Re: ORDER BY problem
Posted: Mon Jul 27, 2009 5:01 pm
by califdon
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
Posted: Mon Jul 27, 2009 5:15 pm
by Foxy999
califdon 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.
Thanks, that's what I thought I had to do
Re: ORDER BY problem
Posted: Mon Jul 27, 2009 5:17 pm
by VladSun
califdon wrote:Your id field is obviously ASCII text, not a number.
Nice catch

I feel a little bit stupid now

Re: ORDER BY problem
Posted: Mon Jul 27, 2009 5:19 pm
by superdezign
VladSun wrote:Nice catch

I feel a little bit stupid now

You'll notice it the next time, though. :3
Re: ORDER BY problem
Posted: Mon Jul 27, 2009 5:32 pm
by califdon
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
Posted: Mon Jul 27, 2009 5:34 pm
by VladSun
superdezign wrote:VladSun wrote:Nice catch

I feel a little bit stupid now

You'll notice it the next time, though. :3
Well... obviously (for me) I prefer the "linear iteration" model of debugging someone's else issues - i.e. the KISS principle applied.
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"

That's why I feel stupid

It wasn't a "complicated" issue.
And yes - next time I won't miss it, ha-ha-ha

Re: ORDER BY problem
Posted: Mon Jul 27, 2009 5:36 pm
by VladSun
califdon 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!

I second that

This forum teaches much more about life (and related) skills than PHP ones

Thanks for that!