Ad Code

How does ORDER BY FIELD() in MySQL work internally

For the record

SELECT * FROM mytable WHERE id IN (1,2,3,4) ORDER BY FIELD(id,3,2,1,4);
 

should work as well because you do not have to order the list in the WHERE clause

As for how it works,

  • FIELD() is a function that returns the index position of a comma-delimited list if the value you are searching for exists.
  • The ORDER BY values are evaluated by what FIELD() returns
 

Post a Comment

0 Comments