Possible Duplicate:
Ordering by the order of values in a SQL IN() clause可能的重复:按照SQL in()子句中的值的顺序排序
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?
可以根据IN子句的内容来订购结果吗?
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
)
...
1 个解决方案
#1
5
The IN
clause defines a set, and a set in mathematics has no order.
IN子句定义了一个集合,而数学中的集合没有顺序。
However there seems to be a workaround for MySQL by using the FIELD() function:
然而,使用FIELD()函数似乎可以解决MySQL的问题:
- Ordering by the order of values in a SQL IN() clause
- 按照SQL in()子句中的值的顺序排序
#1
5
The IN
clause defines a set, and a set in mathematics has no order.
IN子句定义了一个集合,而数学中的集合没有顺序。
However there seems to be a workaround for MySQL by using the FIELD() function:
然而,使用FIELD()函数似乎可以解决MySQL的问题:
- Ordering by the order of values in a SQL IN() clause
- 按照SQL in()子句中的值的顺序排序