Flash Thunder
Flash Thunder

Reputation: 12036

MySQL order when using IN ()

I am getting some records from sorted table and would like to ask some other table for records with the same ... lets say ... id.

SELECT * FROM duckies WHERE fluffy_id IN (<array_of_fluffy_ids>) ...

Is there any way to order the query result exactly the same way as fluffy_ids in IN() clause?

Upvotes: 1

Views: 61

Answers (1)

Alma Do
Alma Do

Reputation: 37365

Yes, there is. Use FIELD() function:

SELECT 
  * 
FROM 
  duckies 
WHERE 
  fluffy_id IN (<array_of_fluffy_ids>) 
ORDER BY 
  FIELD(fluffy_id, <array_of_fluffy_ids>)

Upvotes: 7

Related Questions