从品牌网站建设到网络营销策划,从策略到执行的一站式服务
今天就跟大家聊聊有关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();
错误写法
//错误写法 $data = Content::whereIn('id',$ids) ->select('id') ->orderByRaw("FIND_IN_SET('id', "' . implode(",", $ids) . '"' . ")") ->get(); //该写法查询顺序是按照id大小正序排序
原因解析
//正确写法的sql语句为 select `id` from `contents` order by FIND_IN_SET(id, "5,6,7,4,2,1") asc //错误写法的sql语句为 select `id` from `contents` order by 'FIND_IN_SET(id, "5,6,7,4,2,1")' asc //或者 select `id` from `contents` order by `FIND_IN_SET(id, "5,6,7,4,2,1")` asc //FIND_IN_SET()方法外面不要添加任何符号
看完上述内容,你们对laravel 模型查询按照whereIn排序有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
咨询相关问题或预约面谈,可以通过以下方式与我们联系
成都网站建设公司地址:成都市青羊区太升南路288号锦天国际A座10层 建设咨询400-028-6601
成都快上网科技有限公司-四川网站建设设计公司 | 蜀ICP备19037934号 Copyright 2020,ALL Rights Reserved cdkjz.cn | 成都网站建设 | © Copyright 2020版权所有.
专家团队为您提供成都网站建设,成都网站设计,成都品牌网站设计,成都营销型网站制作等服务,成都建网站就找快上网! | 成都网站建设哪家好? | 网站建设地图