0

希望结果与传递给 IN 条件的顺序相同:

SELECT * FROM `table_x` WHERE `id` IN (30, 12, 1001, 24);

得到:

编号 | 字段_a | 字段_b
--------------------------
12 | ... | ...
24 | ... | ...
30 | ... | ...
1001 | ... | ...

期待:

编号 | 字段_a | 字段_b
--------------------------
30 | ... | ...
12 | ... | ...
1001 | ... | ...
24 | ... | ...
4

2 回答 2

2
SELECT * FROM `table_x` 
WHERE `id` IN (30, 12, 1001, 24) 
order by field(id, 30, 12, 1001, 24)
于 2013-10-31T10:49:00.353 回答
1

FIELD函数的参考

SQLFiddle

SELECT * 
  FROM `table_x` 
  WHERE `id` IN (30, 12, 1001, 24) 
  ORDER BY FIELD(id, 30, 12, 1001, 24);

你会得到输出为

|   ID | FIELD_A | FIELD_B |
|------|---------|---------|
|   30 |     ... |     ... |
|   12 |     ... |     ... |
| 1001 |     ... |     ... |
|   24 |     ... |     ... |
于 2013-10-31T10:51:43.053 回答