2

I have a query like this:

SELECT title,id FROM table1 WHERE id IN ('2','7','4','10')

The result set is ordered by id by default, but I need it in the exact order of numbers in above set.

How can I achieve this?

Mat
  • 202,337
  • 40
  • 393
  • 406
hd.
  • 17,596
  • 46
  • 115
  • 165

1 Answers1

5

The FIELD() function should be able to do this:

SELECT
    title, id
FROM
    table1
WHERE
    id IN ('2', '7', '4', '10')
ORDER BY FIELD(id, '2', '7', '4', '10')

See also MySQL sort after argument in IN().

Community
  • 1
  • 1
jensgram
  • 31,109
  • 6
  • 81
  • 98