我正在尝试进行这样的搜索:
从问题中选择 * WHERE id IN (4, 5, 1)
但是,结果是按 ID 排序的
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
| id | question | answer | notes | difficulty | toggle |
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
| 1 | VAR is much talked about in football - what does VAR stand for? | Video Assistant Referee | | Medium | 1 |
| 4 | Which large construction company has gone into receivership with debts of £1.5 billion? | Carillion | | Easy | 0 |
| 5 | What does PFI stand for in PFI contracts? | Private Finance Initiative | | Easy | 1 |
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
但是,我需要按数组顺序排列结果。我在某处发现了以下内容:
SELECT * FROM questions WHERE id IN (4, 5, 1) ORDER BY FIND_IN_SET(id, '4, 5, 1');
这有点工作,但相反返回它:
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
| id | question | answer | notes | difficulty | toggle |
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
| 1 | VAR is much talked about in football - what does VAR stand for? | Video Assistant Referee | | Medium | 1 |
| 5 | What does PFI stand for in PFI contracts? | Private Finance Initiative | | Easy | 1 |
| 4 | Which large construction company has gone into receivership with debts of £1.5 billion? | Carillion | | Easy | 0 |
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
然后,建议的解决方案是在末尾添加 DESC
SELECT * FROM questions WHERE id IN (4, 5, 1) ORDER BY FIND_IN_SET(id, '4, 5, 1') desc;
它返回这个,我根本没有得到
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
| id | question | answer | notes | difficulty | toggle |
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
| 4 | Which large construction company has gone into receivership with debts of £1.5 billion? | Carillion | | Easy | 0 |
| 1 | VAR is much talked about in football - what does VAR stand for? | Video Assistant Referee | | Medium | 1 |
| 5 | What does PFI stand for in PFI contracts? | Private Finance Initiative | | Easy | 1 |
+----+--------------------------------------------------------------------------------------------+----------------------------+-------+------------+--------+
为什么是 4, 1, 5 而不是 4, 5, 1?如何让它返回 4, 5, 1?
干杯
最佳答案
您应该使用ORDER BY FIELD()
或ORDER BY CASE WHEN...
就您而言,第一个可能是最简单的
SELECT *
FROM questions
WHERE id IN (4, 5, 1)
ORDER BY FIELD(id, 4, 5, 1)
关于MySQL - IN(数组)ORDER BY 原始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022924/