Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

" where in " mysql clause

Tags:

php

mysql

i used a mysql query where there is "where id in ("22,20,21") " but when i get the result its like array ordered in asecending order ie 20,21,20

but i need it in the same order as i gave the clause

like image 426
Anish Joseph Avatar asked Nov 30 '10 06:11

Anish Joseph


1 Answers

i think you should be able to use the FIELD keyword like so:

SELECT * FROM table
WHERE id in (22,20,21)
ORDER BY FIELD(id,22,20,21);

this is mysql specific, and seems magical, but it works.

like image 120
nathan gonzalez Avatar answered Nov 14 '22 20:11

nathan gonzalez