Re: Custom Sort Order in MySQL
| From: | (Michael Mauch) | Date: | Sat, 19 Oct 2002 20:27:29 +0000 |
| Subject: | Re: Custom Sort Order in MySQL | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-22523@lists.php.net to get a copy of this message | ||
Steve Vernon <steve@extremewattage.co.uk> wrote:
> Just wondering what is the best way to get some data out of a
> database in cutom order.
>
> They all have grades like A*, A, B, C and some are grade Secret.
>
> If I sort them by grade secret is at the bottom, I would like this
> at the top. I realise I could get all the Secret's, then A*, then A
> as seperate calls, but id prefer to get them all in the correct
> order as I belive this will be faster.
You could use a case construct like this:
select x, y, z,
case grade
when 'Secret' then 1
when 'A*' then 2
when 'A' then 3
when 'B' then 4
when 'C' then 5
end
as sorted_grade
from your_table
order by sorted_grade;
Regards...
Michael
P.S.: Umm, can you turn off HTML in your mails, please?