Ever wanted to sort a resultset of data, by value, but I wanted a couple of exceptions to appear at the top?

SELECT country, population, 
CASE country
WHEN 'United Kingdom' THEN 0
WHEN 'United States' THEN 1
WHEN 'New Zealand" THEN 2
ELSE country
END AS countrySort
FROM countryList 
ORDER BY countrySort ASC;

This will sort the results by country, but with UK, US, NZ as the top three.

By admin

One thought on “MySQL – Order by certain value first”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.