Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IN and ordering?

Tags:

mysql

Here's my query:

SELECT * FROM article WHERE id IN (300, 400, 100)

But it always orders the articles the same, by id.

So I would get out for the above:

id
100
300
400

Is there a way to get the results out in the order that they were requested in the IN statement? eg.

id
300
400
100
like image 981
panthro Avatar asked Jul 24 '14 13:07

panthro


1 Answers

You can try something like this. Use the FIELD() function.

SELECT * FROM article 
WHERE id IN (300, 400, 100) 
ORDER BY FIELD(id, 300, 400, 100);
like image 84
evilone Avatar answered Sep 23 '22 14:09

evilone