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

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

 Leave a Reply

(required)

(required)

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>