MySQL ORDER BY IN() [英] MySQL ORDER BY IN()

查看:32
本文介绍了MySQL ORDER BY IN()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个 ID 的 PHP 数组.这些号码已被订购.

I have a PHP array with numbers of ID's in it. These numbers are already ordered.

现在我想通过 IN() 方法获取我的结果,以获取所有 ID.

Now i would like to get my result via the IN() method, to get all of the ID's.

但是,这些 ID 应该像在 IN 方法中一样排序.

However, these ID's should be ordered like in the IN method.

例如:

IN(4,7,3,8,9)  

应该给出如下结果:

4 - Article 4  
7 - Article 7  
3 - Article 3  
8 - Article 8  
9 - Article 9

有什么建议吗?也许有一个功能可以做到这一点?

Any suggestions? Maybe there is a function to do this?

谢谢!

推荐答案

我想你可能正在寻找函数 FIELD -- 虽然通常被认为是一个字符串函数,但它也适用于数字!

I think you may be looking for function FIELD -- while normally thought of as a string function, it works fine for numbers, too!

ORDER BY FIELD(field_name, 3,2,5,7,8,1)

这篇关于MySQL ORDER BY IN()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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