Laravel find in set排序

Laravel find in set排序

做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列。
例如,用户状态有四种:

0=>未激活;1=>正常;2=>禁用;3=>软删除

现在的需求是,我要按照:正常->未激活->禁用->删除;这个顺序来进行排序,同时按照注册时间降序,网上查了很多资料,国内提到这个的很少,在stackOverFlow上找到了答案!
先上解决方案:

public function index($customer_type = null) {
    $search = request('search');
    $perPage = request('perPage') ? request('perPage') : 10;
    $customer_type = $customer_type ? $customer_type : request('customer_type');
    // \DB::enableQueryLog();
    $data = Customer::select(['id', 'email', 'user_name', 'nick_name', 'status', 'phone', 'create_time'])
        ->where('customer_type', '=', $customer_type)
        ->where(function ($query) use ($search) {
            if ($search) {
                $query->where('user_name', 'like binary', '%' . $search . '%')
                    ->orWhere('nick_name', 'like binary', '%' . $search . '%')
                    ->orWhere('phone', 'like binary', '%' . $search . '%')
                    ->orWhere('email', 'like binary', '%' . $search . '%');
            }
        })
        ->orderByRaw("FIELD(status, " . implode(", ", [1, 2, 0, 3, 4]) . ")")
        ->orderBy('create_time', 'desc')
        ->paginate($perPage);
    // $query = \DB::getQueryLog();
    // dd($data);
    //追加额外参数,例如搜索条件
    $appendData = $data->appends(array(
        'search' => $search,
        'perPage' => $perPage,
    ));
    return view('admin/customer/customerList', compact('data'));
}

  打印出来的sql语句如下:

Laravel find in set排序

参考了以下链接:
https://stackoverflow.com/questions/42068986/laravel-weird-behavior-orderbyrawfield

https://stackoverflow.com/questions/34244455/how-to-use-not-find-in-set-in-laravel-5-1

https://stackoverflow.com/questions/35594450/find-in-set-in-laravel-example/35594503

find_in_set 复杂应用:

public function get_teacher_list($timeType, $name, $perPage = 10, $personality = 0, $teachingStyle = 0, $ageType = 0)
{
    // \DB::enableQueryLog();
    $result_data = DB::table('teacher_info as ti')
        ->select('ti.*')
        ->join('customer', 'customer.id', '=', 'ti.customer_id')
        ->where(function ($query) use ($personality) {
            if (trim($personality)) {
                $query->whereRaw("find_in_set($personality,ti.label_ids)");
            }
        })
        ->where(function ($query) use ($teachingStyle) {
            if (trim($teachingStyle)) {
                $query->whereRaw("find_in_set($teachingStyle,ti.label_ids)");
            }
        })
        ->where(function ($query) use ($ageType) {
            if (trim($ageType)) {
                $ageType = explode('-', $ageType);
                $query->whereRaw("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 between $ageType[0] and $ageType[1]");
            }
        })
        ->where(function ($query) use ($timeType) {
            //1本周,2下周
            if ($timeType == 1) {
                $query->where('ti.can_appointment_1', 1);
            } elseif ($timeType == 2) {
                $query->where('ti.can_appointment_2', 1);
            } else {
                $query->where('ti.can_appointment_1', '>', 0)
                    ->orWhere('ti.can_appointment_2', '>', 0);
            }
        })
        ->where(function ($query) use ($name) {
            if (trim($name)) {
                $query->where('ti.chinese_name', 'like', '%' . $name . '%')
                    ->orWhere('ti.english_name', 'like', '%' . $name . '%');
            }
        })
        ->where('ti.status', 1)
        ->orderBy('ti.total_teach_num', 'desc')
        ->orderBy('ti.total_star_num', 'desc')
        ->orderBy('ti.satisfaction', 'desc')
        ->orderBy('ti.comment_num', 'desc')
        ->orderBy('ti.english_name', 'asc')
        ->paginate($perPage);
    // dd($result_data, \DB::getQueryLog());

    return $result_data;
}

  专门拿出来看一下:

$ids = array(1,17,2);

$ids_ordered = implode(',', $ids);

$items = User::whereIn('id', $ids)
 ->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))
 ->get();

  转载:https://blog.csdn.net/zhezhebie/article/details/78357354

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112320.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ImageView.ScaleType设置,适配列表item

    ImageView.ScaleType设置,适配列表item

    2021年9月30日
    43
  • EXCEL 出错 8000401a

    EXCEL 出错 8000401a检索COM类工厂中CLSID为{00024500-0000-0000-C000-000000000046}的组件时失败,原因是出现以下错误:8000401a先用骨哥狗了一会,没解决方案,又用摆渡,也没找到方法,最后还是看微软的帮助解决了问题:1.打开DCOM配置,取消交互式用户,使用启动用户2.安全中附足够权限,不知道用户是谁就写Everyonehttp://suppo…

    2022年7月25日
    9
  • 图解排序算法(三)之堆排序

    图解排序算法(三)之堆排序预备知识堆排序堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序。首先简单了解下堆结构。堆堆是具有以下性

    2022年7月4日
    19
  • ajax怎么解决报414,关于c#:HTTP错误414。请求URL太长。 asp.net

    ajax怎么解决报414,关于c#:HTTP错误414。请求URL太长。 asp.net我收到错误”HTTP错误414。请求URL太长”。从下面的文章中,我了解到这是由于查询字符串很长所致:在web.config中,我有maxQueryStringLength=”2097151″。这是最大值吗?为了解决此问题,我应该在web.config中设置maxUrl吗?如果是这样,支持的最大值是多少?我该怎么办才能解决此错误?是否可以将URL中的某些长字符串替换为整数或Guid?如果…

    2022年6月3日
    43
  • pix2pix论文(pix是什么意思)

    图像翻译,指从一副图像到另一副图像的转换。可以类比机器翻译,一种语言转换为另一种语言。下图就是一些典型的图像翻译任务:比如语义分割图转换为真实街景图,灰色图转换为彩色图,白天转换为黑夜……本文主要介绍图像翻译的三个比较经典的模型pix2pix,pix2pixHD,vid2vid。pix2pix提出了一个统一的框架解决了各类图像翻译问题, pix2pixHD则在pix2pix的基础上,较好的解决了高分辨率图像转换(翻译)的问题, vid2vid则在pix2pixHD的基础上,较好的

    2022年4月10日
    115
  • SpringBoot自动配置的原理及实现

    SpringBoot自动配置的原理及实现SpringBoot自动配置的实现原理SpringBoot的核心就是自动配置,自动配置又是基于条件判断来配置Bean。关于自动配置的源码在spring-boot-autoconfigure-2.0.3.RELEASE.jar回顾配置属性在通常需要我们在property中配置信息时,通常使用@ConfigurationProperties(pefix=“前缀”)注解的方式从配置文件中获取配置…

    2022年5月5日
    40

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号