Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

May 022011

Order by predefined order in MySQL


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);


 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>