MySQL - IN(数组)ORDER BY 原始

标签 mysql

我正在尝试进行这样的搜索:

从问题中选择 * 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)

SQLfiddle

关于MySQL - IN(数组)ORDER BY 原始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022924/

相关文章:

php - mysql查询中未定义索引

数据库 |获取数据库错误,无效使用组功能

mysql - 在 java 类中访问我的 docker mysql 容器

java - 如何从一个jsp页面过渡到另一个?

mysql - Ruby on Rails 无法使用 MySQL 创建/使用应用程序

javascript - Php 计时器没有停止在 00 :00

php - CodeIgniter 使用 ActiveRecord 转义查询.. 不希望它

java - 使用 Spring JPA 将阿拉伯语文本保存到 mysql

php - 如何使用 php 每行显示四个图像并且行可以是任何 no

php - 如何检查用户之前回复的帖子是否收到新回复?