MySql where In 加 order by排序

14. August 2019 DataBase 0

有时,我们希望根据where in data 数据顺序进行筛选,Laravel方式,如下:

$college_ids = [...]
$order_ids = implode(",", $college_ids);
$videos = CollegeVideo::whereIn("college_id", $college_ids)->orderByRaw("FIELD(college_id,$order_ids)")->paginate(20);
MySql生成的代码:
SELECT * FROM `MyTable`WHERE `id` IN (11,1,111) ORDER BY FIELD(`id`, 11,1,111);

Leave a Reply

Your email address will not be published. Required fields are marked *