php mysql 按获取的顺序对结果进行排序 [英] php mysql sort results by order fetched

查看:32
本文介绍了php mysql 按获取的顺序对结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用IN语句时按照数组中id的顺序对得到的结果进行排序:

How can I order the results I get by the order of the ids in the array when using IN statement:

SELECT id,name from table WHERE id IN (23, 33, 55, 2)

SELECT id,name from table WHERE id IN (23, 33, 55, 2)

我想得到:

+----+---------+
| id | name    |
+----+---------+
| 23 | name1   |
| 33 | name2   |
| 55 | name3   |
|  2 | name4   |
+----+---------+

按这个顺序

推荐答案

只需在查询的末尾添加:

simply add this at the end of your query:

order by field(id, 23, 33, 55, 2)

这篇关于php mysql 按获取的顺序对结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆