Laravel - 如何在查询中获取计数数据?

时间:2018-04-29 01:55:27

标签: database laravel

这是mysql中的查询

  

选择e_id,h_name,   d_name,drf_name,credit_num,is_required,e_semester,stdate,endate,c_month,NUM   来自(SELECT electcourse.id e_id,   h_id,d_id,ref_id,e_semester,stdate,endate,c_month,count(e_id)as num   来自electcourse = period的{​​{1}}离开加入p_idid左连接   hospitalxdepartment上的hd_id = hospitalxdepartment。id离开加入   关于选举的board id = e_id其中st_id =' admin'通过...分组   e_id)我在hospital = hospital。h_id左连接上离开加入id   department =部门d_id id离开加入   departmentreference = ref_id上的drfid df_semester = 105

这是我在laravel中的查询,但它收到了一些错误消息。

public function get_course($st_id,$semester){
        $from=DB::table('electcourse')
                ->selectRaw('electcourse.id as e_id','h_id','d_id','ref_id','e_semester','stdate','endate','c_month','count(e_id) as num',)
                ->leftjoin('period','p_id','period.id')
                ->leftjoin('hospitalxdepartment','hd_id','hospitalxdepartment.id')
                ->leftjoin('board','electcourse.id','e_id')
                ->where('st_id','=',$st_id)
                ->where('e_semester','=',$semester)
                ->groupBy('e_id');
        $query=DB::query()
                ->select('e_id','h_name','d_name','drf_name','credit_num','is_required','e_semester','stdate','endate','c_month')
                ->fromSub($from, 'T')
                ->leftjoin('hospital','h_id','hospital.id')
                ->leftjoin('department','d_id','department.id')
                ->leftjoin('departmentreference','ref_id','drfid')
                ->where('df_semester','=',$semester)->orderBy('c_month')->get();
        return $query;

    }

如果我没有在" select"中使用count,我确保它会起作用。串。 有谁知道如何使用正确的计数方法修复它?

  

错误消息:" Class App \ Repositories \ BoardRepository没有   存在"   这意味着DB格式错误。

1 个答案:

答案 0 :(得分:0)

我找到了一种生成正确查询的方法,但它会收到错误消息。

查询功能

public function get_course($st_id,$semester){

    $from=DB::table('electcourse')
            ->select('electcourse.id as e_id','h_id','d_id','ref_id','e_semester','stdate','endate','c_month',DB::raw('count(e_id) as num'))
            ->leftjoin('period','p_id','period.id')
            ->leftjoin('hospitalxdepartment','hd_id','hospitalxdepartment.id')
            ->leftjoin('board','electcourse.id','e_id')
            ->where('st_id','=',$st_id)
            ->where('e_semester','=',$semester)
            ->groupBy('e_id');
    $query=DB::query()
            ->select('e_id','h_name','d_name','drf_name','credit_num','is_required','e_semester','stdate','endate','c_month')
            ->fromSub($from, 'T')
            ->leftjoin('hospital','h_id','hospital.id')
            ->leftjoin('department','d_id','department.id')
            ->leftjoin('departmentreference','ref_id','drfid')
            ->where('df_semester','=',$semester)->orderBy('c_month')->get();
    return $query;

}

错误讯息:

  

" SQLSTATE [42000]:语法错误或访问冲突:1055   ' yangming567.electcourse.id'不在GROUP BY中(SQL:选择e_id,   h_named_namedrf_namecredit_numis_required,   来自e_semesterstdateendatec_month   electcourseide_idh_idd_idref_ide_semester,   stdateendatec_month,计数(e_id)为来自electcourse的数字   在period = p_id上离开加入periodid左连接   hospitalxdepartment = hd_id上的hospitalxdepartmentid离开   在board上加入electcourseid = e_id其中st_id = admin和   e_semester = 105 e_id组{} T左加入hospital   h_id = hospitaliddepartment =左加入d_id =   departmentiddepartmentreference =上离开加入ref_id   drfid其中df_semester = 105订单c_month asc)   但是,我将此错误消息查询复制到Mysql,这是工作!!!!   有人知道如何在laravel中修复我的查询吗?