PDA

View Full Version : How do you sort Alphabetically but put 'Other' last?


RedHouse
08-04-2009, 03:09 AM
I have a table of categories which I want to list in alphabetical order. However there is always a category called 'Other' which I want to always be displayed last on the list? Is there any simple way of doing this with MYSQL. I currently have the statement

SELECT * FROM categories ORDER BY name ASC;

but this displays the category 'Other' half way down the list.

Old Pedant
08-04-2009, 04:56 AM
Thus:

SELECT *
FROM categories
ORDER BY IF( name = 'Other', 2, 1 ) ASC, name ASC

or, of course, if it's clearer:

SELECT *
FROM categories
ORDER BY IF( name != 'Other', 1, 2 ) ASC, name ASC

RedHouse
08-04-2009, 05:14 AM
Thank you for this simple snippet, you are pro.