我在laravel框架中使用了以下查询
$student_attendance_search_result = DB::table('tbl_student_attendance')
->join('tbl_student_admission', 'tbl_student_attendance.student_id', '=', 'tbl_student_admission.student_id')
->select(DB::raw('tbl_student_attendance.*,tbl_student_admission.student_id,tbl_student_admission.student_full_name_english,tbl_student_admission.class,tbl_student_admission.section,tbl_student_admission.roll_no, count(case when attendance_status ="Absent" then 1 end) as total_absent,count(case when attendance_status ="Present" then 1 end) as total_present,count(case when attendance_status ="Leave" then 1 end) as total_leave,count(distinct date) as total_class'))
->where('tbl_student_attendance.academic_year', $academic_year)
->where('tbl_student_admission.class', $classs)
->where('tbl_student_admission.section', $section)
->where('tbl_student_attendance.month', $month)
->groupBy('tbl_student_admission.student_id')
->get();
当我试图运行它时,它抛出以下异常
sqlstate[42000]:语法错误或访问冲突:1055“db\u smsfinal1user.tbl\u student\u attention.student\u attention\u id”不在分组依据中(sql:select) tbl_student_attendance
.*, tbl_student_admission
. student_id
, tbl_student_admission
. student_full_name_english
, tbl_student_admission
. class
, tbl_student_admission
. section
, tbl_student_admission
. roll_no
,count(case when attention\u status=“absent”then 1 end)as total\u absent,count(case when attention\u status=“present”then 1 end)as total\u present,count(case when attention\u status=“leave”then 1 end)as total\u leave,count(distinct date)as total\u class from tbl_student_attendance
内部连接 tbl_student_admission
在 tbl_student_attendance
. student_id
= tbl_student_admission
. student_id
哪里 tbl_student_attendance
. academic_year
=2018年 tbl_student_admission
. class
=2和 tbl_student_admission
. section
=无和 tbl_student_attendance
. month
=9月分组 tbl_student_admission
. student_id
)
知道我做错了什么吗?
1条答案
按热度按时间wwodge7n1#
使用db::raw作为选择参数
DB::select( DB::raw('student_id,count(case when attendance_status ='Absent' then 1 end) as absent_count,count(case when attendance_status ='Present' then 1 end) as present_count,count(case when attendance_status ='Leave' then 1 end) as leave_count,count(distinct date) as Tot_count') )->from(..)->groupBy(...);