I have the table ‘category’ with two columns: ‘id’ and ‘name’.
I Want to display the names and order the table by alphabet, but if the name “other” is present, i want to set it to be the last row.
Is there a way to achieve this without creating another column that marks the position?
ORDER BY name DESC { something to set name = "other" to the end }
THANKS!
Using a
CASE, you can apply ordering conditionally in theORDER BY. Rows wherename <> 'other'are conditionally assigned a0which sorts ahead of those matchingotherwhich are assigned a1. Then it is sub-ordered byname.You use
name DESCin your example, but your description implies you want it sorted in ascending alphabetical order. If that is incorrect and you do want it descending, usename DESCinstead.Note that for MySQL, the boolean expression
(name = 'other')will evaluate to a 0 or 1. So this can be simplified to omit theCASE:This is not portable to any RDBMS though.