我正在尝试像这样进行搜索:
SELECT * FROM questions 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?
欢呼
答案 0 :(得分:2)
您应该使用ORDER BY FIELD()
或ORDER BY CASE WHEN...
在您的情况下,第一个可能是最简单的
SELECT *
FROM questions
WHERE id IN (4, 5, 1)
ORDER BY FIELD(id, 4, 5, 1)
答案 1 :(得分:0)
drop table if exists t;
create table t(id int, question varchar(100));
insert into t values
( 1 , 'VAR is much talked about in football - what does VAR stand for?' ),
( 4 , 'Which large construction company has gone into receivership with debts of £1.5 billion?' ),
( 5 , 'What does PFI stand for in PFI contracts?' );
select *
from t
order by case when id = 4 then 1
when id = 1 then 2
else 3
end;
+------+------------------------------------------------------------------------------------------+
| id | question |
+------+------------------------------------------------------------------------------------------+
| 4 | Which large construction company has gone into receivership with debts of £1.5 billion? |
| 1 | VAR is much talked about in football - what does VAR stand for? |
| 5 | What does PFI stand for in PFI contracts? |
+------+------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)