22

Possible Duplicate:
Ordering by the order of values in a SQL IN() clause

With a query such as:

SELECT * FROM images WHERE id IN (12,9,15,3,1)

is it possible to order the results by the contents of the IN clause?

The result I'm looking for would be something like:

[0] => Array
    (
        [id] => 12
        [file_name] => foo
    )   
[1] => Array
    (
        [id] => 9
        [file_name] => bar
    )   
[2] => Array
    (
        [id] => 15
        [file_name] => baz
    )
...
Community
  • 1
  • 1
Jeff
  • 2,794
  • 8
  • 30
  • 35

1 Answers1

10

The IN clause defines a set, and a set in mathematics has no order.

However there seems to be a workaround for MySQL by using the FIELD() function:

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443