laravel 模型查詢按照whereIn排序


$ids = [5,7,3,1,2];
$data = Content::whereIn('id',$ids)
->select('id')
->get();
//查詢結果是想按照wherein的順序排序
//正確寫法
$data = Content::whereIn('id',$ids)
->select('id')
// ->orderBy(\DB::raw('FIND_IN_SET(id, "' . implode(",", $ids) . '"' . ")"))
// ->orderBy(DB::raw("FIND_IN_SET(id, '" . implode(',', $ids) . "'" . ')'))
// ->orderByRaw("FIND_IN_SET(id, '" . implode(',', $ids) . "'" . ')')
->orderBy(\DB::raw('FIND_IN_SET(id, "' . implode(",", $ids) . '"' . ")"))
->get();

 

執行的sql

select `id`, `name`, `wx_avatar` from `wm_user` where (`is_delete` = "0") and `id` in ("1643", "100001", "7036") order by FIND_IN_SET(id, '1643,100001,7036')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM