$resData = (new \yii\db\Query())
->select('*')
->from('product')
->where(['id' => $productIdsArray])
->all();
//查出的结果是升序排列的要想实现指定数组内的排序顺序可以使用下面的方法
$productIds = implode(',' , $productIdsArray);
$orderBy = (new \yii\db\Query())
->select('*')
->from('product')
->where(['id' => $productIdsArray])
->orderBy([new \yii\db\Expression("FIELD (id, $productIds)")])
->all();
yii2 in 集合查询排序失效解决方法
本文详细介绍了如何在Yii框架中使用数据库查询来获取特定顺序的产品数据。通过展示具体的代码示例,文章解释了如何利用FIELD函数对查询结果进行排序,以确保返回的数据符合预设的ID顺序。

被折叠的 条评论
为什么被折叠?



