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

Database 如何在查询中获取计数数据?,database,laravel,Database,Laravel,这是mysql中的查询 选择e_id、h_name、, d_名称、drf_名称、学分数、是否必填、e_学期、stdate、endate、c_月份、num 从select electcourse.id e_id, h_id,d_id,ref_id,e_学期,stdate,endate,c_月,counte_id作为num 从p_id=period.id上的左连接周期开始左连接 hospitalxdepartment on hd_id=hospitalxdepartment.id左连接 在elect

这是mysql中的查询

选择e_id、h_name、, d_名称、drf_名称、学分数、是否必填、e_学期、stdate、endate、c_月份、num 从select electcourse.id e_id, h_id,d_id,ref_id,e_学期,stdate,endate,c_月,counte_id作为num 从p_id=period.id上的左连接周期开始左连接 hospitalxdepartment on hd_id=hospitalxdepartment.id左连接 在electcourse.id=e_id,其中st_id='admin'分组依据 e_id T left join hospital on h_id=hospital.id left join d_id上的部门=department.id左连接 参考id=drfid上的部门参考,其中df=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,我会确保它可以正常工作。 有人知道如何用正确的计数方法修复它吗

错误消息:类App\Repositories\BoardRepository不存在 存在 这意味着DB格式是错误的


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

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;

    }
查询函数

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”不在SQL组中:请选择e_id, h_名称、d_名称、drf_名称、信用编号为必填项, e_学期、stdate、endate、c_月(从选择开始) 选择course.id作为e_id,h_id,d_id,ref_id,e_学期, stdate、endate、c_month、counte_id作为课程中的num p_id上的左连接周期=period.id左连接 hospitalxdepartment on hd_id=hospitalxdepartment.id左 在electcourse.id=e_id上加入董事会,其中st_id=admin和 e_学期=105组,由e_id确定,因为T离开并于 h_id=医院id离开,在d_id上加入科室= department.id左加入部门参考号上的参考号= drfid,其中df_学期=105订单,按c_月asc 但是,我将这个错误消息查询复制到Mysql,这是工作!!!! 有人知道如何解决我在laravel的问题吗


你能告诉我们错误信息是怎么说的吗?“这很可能很重要。”切亚约兹:当然。错误消息您发布的错误消息与此查询或您共享的这部分代码无关。你的问题在别的地方。它应该给你一个文件和行号,你的代码试图使用一个名为BoardRepository的类。我想在我的laravel查询中有一些错误格式。如果我在DB查询中有一些格式错误,将显示此错误消息@Ceejayoz您与我们共享的错误消息与此查询或代码无关。同样,错误消息应该会告诉您正在尝试使用BoardRepository类,但Laravel正在app/Repositories/BoardRepository.php中查找类为BoardRepository{…}的文件,但找不到该文件。