Laravel MySQL如何以与whereIn子句相同的顺序排序结果

栏目: PHP · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/26176245/laravel-mysql-how-to-order-results-in-the-same-order-as-in-wherein-clause

我有两个查询,第一个给我一个id数组,这是一个特定的顺序.然后我将这个ID数组传递给第二个查询,如下所示:

Operation::whereIn('id', $ids)->get();

但是当我输出该查询的结果时,顺序已经改变,如果数组$ids类似于(4,2,6,9),这是我想要结果的顺序,输出将给我2 ,4,6,9.我怎么能避免这种情况?

MySQL的 排序 方式与where子句中的顺序相同:
$ids; // array of ids
$placeholders = implode(',',array_fill(0, count($ids), '?')); // string for the query

Operation::whereIn('id', $ids)
   ->orderByRaw("field(id,{$placeholders})", $ids)->get();

翻译自:https://stackoverflow.com/questions/26176245/laravel-mysql-how-to-order-results-in-the-same-order-as-in-wherein-clause


以上所述就是小编给大家介绍的《Laravel MySQL如何以与whereIn子句相同的顺序排序结果》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Beginning iPhone and iPad Web Apps

Beginning iPhone and iPad Web Apps

Chris Apers、Daniel Paterson / Apress / 2010-12-15 / USD 39.99

It seems that everyone and her sister has developed an iPhone App—everyone except you, the hard-working web professional. And now with the introduction of the iPad, you may even feel farther behind. B......一起来看看 《Beginning iPhone and iPad Web Apps》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具