MySQL按数组值排序
16lz
2021-01-22
I need to run a MySQL query where the order is determined by an array value.
我需要运行一个MySQL查询,其中的顺序由数组值决定。
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.
我的数组是可变的,但数组中的值对应于我的数据库表中名为“ID”的字段,因此我希望在ID顺序9,1,4中返回结果。
Array ( [0] => 9 [1] => 1 [2] => 4 )
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.
这在MySQL中是否可行,或者可以在之后使用数组对MySQL $结果进行排序?您可以假设返回的唯一值是数组中的值。
2 个解决方案
#1
61
ORDER BY field(id, 9, 1, 4);
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field
更多相关文章
- 向数组添加一组数据库列的问题
- 如何使用webrequest使用Json数组绘制折线图
- 数据库_MySQL_复杂SQL的书写顺序与执行过程
- MySql SELECT 语句执行顺序
- 如果条件按顺序,Mysql不能使用mysql
- MySQL查询时有时候需要某条记录置顶或者放最后,而其他的记录则按
- javascript判断数组和对象中是否存在某元素
- [JavaScript] 将字符串数组转化为整型数组
- 数组中的值由于未知原因而更改