Returning query results in predefined order

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, MySQL Tutorial  //  No Comments

Is it possible to do a SELECT statement with a predetermined order?

Here’s the SQL code:


SELECT id FROM table WHERE id in (7,2,5,9,8) 
ORDER BY FIND_IN_SET(id,"7,2,5,9,8");

Ordering is done by ORDER BY keywords

FIND_IN_SET will returns the position of id in the second argument given to it, so for the first case above, id of 7 is at position 1 in the set, 2 at 2 and so on

Leave a comment

Share This Post

RSS Wordpress News