Laravel 查询多个字段使用聚合函数

使用SUM 

$data  = TeacherWageProject::leftJoin('teacher_user_wage_project_money','teacher_wage_project.id','=','teacher_user_wage_project_money.wage_project_id')
            ->leftJoin('teacher_user_wage','teacher_user_wage_project_money.wage_id','teacher_user_wage.id')
            ->where($where)->groupBy('teacher_wage_project.id')
            ->select('teacher_wage_project.id','teacher_wage_project.name',DB::raw('SUM(teacher_user_wage_project_money.wage_project_money) as sum_project_money'))
            ->paginate(config('page.per_page')); // 获取数据
        $this->answer(config('code.100000.code'),'success','获取数据成功',$data); // 响应数据

使用COUNT

$data = TeacherClass::leftJoin('parent_user','class.id','=','parent_user.class_id')
            ->leftJoin('parent_sign','parent_user.id','=','parent_sign.parent_id')->where($where)->where('parent_sign.type',1)
            ->where([['parent_sign.create_time','>=',$lastMonthStart],['parent_sign.create_time','<',$lastMonthEnd]])
            ->groupBy('parent_sign.parent_id')
            ->select('parent_user.id','parent_user.class_id','parent_user.user_name','class.class_name',DB::raw('COUNT(parent_sign.parent_id) as studentNum'))
            ->get()->toArray();
        print_r($data);die;

 

你可能感兴趣的:(php,Laravel,框架)