Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL - SELECT IN ( ) ORDER BY IN ( )

If i select like this:

SELECT id FROM users WHERE id IN(3,4,8,1);

This by default will select users in this order

1,
3,
4,
8,

I would like to select them in the same order that i put IN() values so:

   3,
   4,
   8,
   1

is this possible?

like image 301
itsme Avatar asked Nov 06 '13 10:11

itsme


1 Answers

Use FIELD() for that

SELECT id 
FROM users 
WHERE id IN(3,4,8,1)
order by field(id, 3,4,8,1)
like image 51
juergen d Avatar answered Sep 24 '22 18:09

juergen d