Page 1 of 1

Need help! SQL order by

Posted: Sat Aug 09, 2008 2:48 pm
by newbie2php
Hi all

Is this at all possible:

Ordering content from a database table in alphabetical order, BUT the first value to be set by myself?

So, for example:

Name Age

Frank 21
Tom 32
Andy 21
Owen 43

So in this example I would want to order that data so that Owen is first, and then we order by alphabetic values, so the outcomes would be

Owen (this is the value I want to appear first)
Andy (then the rest are outputted in alphabetical order)
Frank
Tom

This is just an example, I have alot more data then that and they are not names and ages.

Re: Need help! SQL order by

Posted: Sat Aug 09, 2008 2:50 pm
by newbie2php
And just to add that this needs to be all in one SQL command

Many thanks :D

Re: Need help! SQL order by

Posted: Sat Aug 09, 2008 9:04 pm
by califdon
Remember, SQL is a logical language, not a mind reader. If there is a logical way to determine the order, you can probably express it in SQL, but if the logic is just "I want Owen to appear first", there is no way to do that in one SQL statement. You could write a UNION query that is really two queries, one appended to the other, the first one specifically selecting ONLY the record you want to appear first and the second selecting everything BUT that record, in some order. Your question sounds like a very peculiar situation, indeed. We might be able to help you more if you could explain WHY you would ever want to do this.

Re: Need help! SQL order by

Posted: Sun Aug 10, 2008 2:42 am
by Apollo
try order by replace(Name,'Owen',NULL)