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

更多相关文章

  1. 向数组添加一组数据库列的问题
  2. 如何使用webrequest使用Json数组绘制折线图
  3. 数据库_MySQL_复杂SQL的书写顺序与执行过程
  4. MySql SELECT 语句执行顺序
  5. 如果条件按顺序,Mysql不能使用mysql
  6. MySQL查询时有时候需要某条记录置顶或者放最后,而其他的记录则按
  7. javascript判断数组和对象中是否存在某元素
  8. [JavaScript] 将字符串数组转化为整型数组
  9. 数组中的值由于未知原因而更改

随机推荐

  1. 关于golang之排序使用
  2. 【发布了Go-carbon1.1.1版本】完善对主流
  3. 关于Go语言的http/2服务器功能及客户端使
  4. 关于Go SQL中的Query、Exec和Prepare使用
  5. 教你使用Golang和lua实现一个值班机器人
  6. gin框架有什么优势
  7. windows10下编译go项目为linux可执行文件
  8. 分享一些为PHPer准备的Go入门知识
  9. 关于 golang 的接口介绍
  10. 教你使用golang实现redis服务器