复杂查询加入,选择,计数和分组

时间:2017-05-06 10:46:13

标签: mysql laravel

我想构建一个查询来匹配来自学生表的会话,并匹配来自记录的exambatch表中的其他数据(学生可能来10次,但在学生表中,它只来一次)。 我的疑问是:

$students_count = DB::table('student')
->join('exambatch', 'exambatch.st_id', '=', 'student.id')
->select('student.*','exambatch.*')
->where('exambatch.b_id','=', $term->id)
->where('exambatch.depid','=', $dp->id)
->where('student.session','=', $tim->id)
->groupBy('exambatch.depid')
->count();

我希望学生能够被部门统计以及考虑会议。 根据每学期升级,学生可以多次注册当前查询对学生进行多次计数,我需要将其计为一个。

如下所示,我们有2个表格。第一个只获取学生信息,而第二个保存学生的学术日志,例如哪个学期?哪个学期?哪个部门?在第二个表格中,我们为每个学期生成一行,例如一个学生已经学了5个学期,在exambatch上他将有5行。现在我想加入这些表格,以便在考虑会话时找出特定术语和部门中有多少学生。问题是学生ID在exambatch表中保存了5次或10次,现在我想查询和计算使用where条件的学生数,所以,我需要计算那些具有相同学生ID的5行,并根据下表生成结果:

Depid | Session | B_id | Count
1     | Morning | 2    | 110
1     | Evening | 2    | 10
2     | Morning | 1    | 105
1     | Morning | 4    | 100

enter image description here enter image description here

0 个答案:

没有答案
相关问题