MySQL sort order by array value(MySQL按数组值排序)
问题描述
我需要运行一个 MySQL 查询,其中的顺序由数组值决定.
I need to run a MySQL query where the order is determined by an array value.
我的数组是可变的,但数组中的值对应于我的数据库表中名为ID"的字段,因此我希望以 ID 顺序 9、1、4 返回结果.
My array is variable but the values in the array correspond to a field in my DB table called 'ID' so I want the result to be returned in the ID order 9, 1, 4.
Array ( [0] => 9 [1] => 1 [2] => 4 )
这在 MySQL 中是否可行,或者是否可以使用数组对 MySQL $result 进行排序?您可以假设唯一返回的值是数组中的值.
Is this possible in MySQL or would it be possible to sort the MySQL $result using the array after? You can assume the only values being returned are those in the array.
推荐答案
ORDER BY field(id, 9, 1, 4);
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
这篇关于MySQL按数组值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL按数组值排序


基础教程推荐
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01