0

Searching a table for precise elements by their ID in a precise order, how can I use this kind of request:

select * from TABLE where ID in (1, 7, 2)

To sort elements in the order I requested them (ID = 1 then ID = 7 then ID = 2) ? Just as the following :

TABLE
ID | field1 | ...
1  | value  | ...
7  | value  | ...
2  | value  | ...

Given of course that the requested order isn't a natural order of one of the fields (can't do order by field to get the result).

tomsihap
  • 1,712
  • 18
  • 29

1 Answers1

1

use FIELDS as below

select * from TABLE where ID in (1, 7, 2) ORDER BY FIELD(ID, 1, 7, 2)
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37