thinkphp5自定义sql排序

先列下目标sql语句

SELECT * FROM `table` WHERE `id` IN  (423603,423605,423606) ORDER BY FIELD(`id`, 423603,423606,423605)

 where id in (1,2,3) 这个条件按以下代码写即可

$query->where('id', 'in', $list_str);

但后面的order实现有些麻烦,找了一圈,可以按如下实现

$list_str = implode(",", $array);
$exp =new \think\db\Expression('field(id,'.$list_str.')');

$query = $query->order($exp);

 

posted on 2020-09-12 16:58  aozima  阅读(456)  评论(0编辑  收藏  举报

导航