第一种
还有一种是 通过mysql内置的函数 find_in_set
如果不可以的话,不要来找我,我没有试过
另外一种
<?php
/**
* Created by.
* User: Jim
* Date: 2020/11/16
* Time: 9:11
*/
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\db\Expression;
class Test extends Controller
{
public function index()
{
$ids = implode(',',[1,4,844,6,900,10]);
$exp = new Expression('field(id,'.$ids.')');
$datas = Db::name('goods')
->whereIn('id',$ids)
->field('id,rank')
->order($exp)
->select();
dump($datas);
}
}
// 详情
public function detail($ids = ''){
$combination = $this->model->get($ids);
// 根据goods—ids 里面的值,按照原来的排序出来
//$combination['goods_ids'] == 1,5,4,7,1,3
$exp = new Expression("field(id,{$combination['goods_ids']})");
$goods = Goods::where("FIND_IN_SET(`id`,'{$combination['goods_ids']}')")
->field('id')
->orderRaw("field(id,{$combination["goods_ids"]})")
// ->order($exp)
->select();
dd(collection($goods)->toArray());
$this->assign('goods',$goods);
$this->assign('combination',$combination);
return $this->view->fetch();
}