Order by predefined order in MySQL
Answer:
Assume you have a SQL statement like:
SELECT id FROM user WHERE id IN (5, 10, 6);
How to make sure the returned rows are in the exact order as 5, 10, 6?
You can do the following:
SELECT id FROM user WHERE id IN (5, 10, 6) ORDER BY FIELD (id, 5, 10, 6);
Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field