I have encountered this problem before and I had found the solution, but now I have completed forgotten how to do it again. Here is the question: I have a query like
SELECT id, name FROM table WHERE id IN (2,4,3,1);
And I want the result to be sorted by the order specified in the IN clause. In order to do this, simply modify the query like the following:
SELECT id, name FROM table WHERE id IN (2,4,3,1) ORDER BY FIELD (id, 2,4,3,1);
The FIELD function returns the position of the first string in the remaining list of strings. Hope this would help someone like me.

Leave a Reply

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