Laravel 4 如何使用Laravel检查值的总数
我有一张“学生考勤表” 我想要这样Laravel 4 如何使用Laravel检查值的总数,laravel-4,count,Laravel 4,Count,我有一张“学生考勤表” 我想要这样 RollNo StudentName 02 13 14 15 16 17 20 Total Presents Total Absents 28 Gokul p p p p a p p 6 1 31 Goku p p p a p p p 6 1 32 G
RollNo StudentName 02 13 14 15 16 17 20 Total Presents Total Absents
28 Gokul p p p p a p p 6 1
31 Goku p p p a p p p 6 1
32 Gok p p p p p a a 5 2
我要把每个学生的出席人数(p)和缺席人数(a)合计起来
我的控制器代码是:
$attendence_tbl = WysAttendancename::where('cls_id',$id)->first();
$wys_attendence_table = $attendence_tbl->attendance_name;
$attendance = DB::table($wys_attendence_table)
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
$stud_attend = DB::table($wys_attendence_table)
->where('amonth',$amonth)
->where('ayear',$ayear)
->get();
我的view.blade php是
@foreach($students as $student)
@if($student->studcls == $id)
<tr>
<td>{{$student->id}}</td>
<td>{{$student->studname}}</td>
@foreach($stud_attend as $stud_attends)
@if($student->id == $stud_attends->studid)
@if($stud_attends->attendence == 1)
<td><font color="green" size="3">p</font></td>
@elseif($stud_attends->attendence == 0)
<td><font color="red" size="3">a</font></td>
@endif
@endif
@endforeach
<td>{{$presentsCountQuery}}</td>
<td>{{$absentsCountQuery}}</td>
</tr>
@endif
@endforeach
进入我的控制器页面,检查礼物和缺席的总数
我把答案拿出来了,答案是int(6)int(6)int(5)
但我无法在查看页面上显示
如何在“我的控制器”页面中检查每个学生的出席总数(出席人数=1)和缺席总数(出席人数=0),以及如何显示要查看页面的值
这是我的桌面wys_出席:
id studid adate amonth ayear acls_id attendence
1 28 02 07 2015 10 1
2 31 02 07 2015 10 0
4 32 02 07 2015 10 1
5 28 30 07 2015 10 0
6 31 30 07 2015 10 1
7 32 30 07 2015 10 1
9 28 31 07 2015 10 1
10 31 31 07 2015 10 1
11 32 31 07 2015 10 1
13 28 06 08 2015 10 1
14 31 06 08 2015 10 0
15 32 06 08 2015 10 1
17 28 07 08 2015 10 0
18 31 07 08 2015 10 1
19 32 07 08 2015 10 1
21 28 08 08 2015 10 1
22 31 08 08 2015 10 1
23 32 08 08 2015 10 0
24 28 12 08 2015 10 1
25 31 12 08 2015 10 1
26 32 12 08 2015 10 0
试试这个@cnbwd:
$attendance = WysAttendancename::select('id', 'studid', 'adate', 'amonth','ayear', 'acls_id','attendence',
DB::raw('(SELECT count(attendence) from your_db WHERE attendence = 1) as count_attendence' ))
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
试试这个@cnbwd:
$attendance = WysAttendancename::select('id', 'studid', 'adate', 'amonth','ayear', 'acls_id','attendence',
DB::raw('(SELECT count(attendence) from your_db WHERE attendence = 1) as count_attendence' ))
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
试试这个
$attendance = DB::table($wys_attendence_table)
->select(DB::raw('count(attendence) as count_attendance'))
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
编辑
您可以从这两个查询中进行一个查询,并在一个foreach中使用它。
大概是这样的:
$countQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS counter'))
->groupBy('studid')
->toSql();
$stud_attend = DB::table($wys_attendence_table . ' AS table')
->leftJoin(DB::raw('(' . $countQuery . ') AS counter_table'), 'table.studid', '=', 'counter_table.studid')
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
编辑2:
$presentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS presents_counter'))
->whereRaw('attendence = 1')
->groupBy('studid')
->toSql();
$absentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS absents_counter'))
->whereRaw('attendence = 0')
->groupBy('studid')
->toSql();
$stud_attend = DB::table($wys_attendence_table . ' AS table')
->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
编辑3:
完整代码:
$attendance = DB::table($wys_attendence_table)
->whereRaw('amonth = "'.$amonth.'"')
->whereRaw('ayear = "'.$ayear.'"')
->groupBy('adate')
->get();
$presentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS presents_counter'))
->whereRaw('amonth = "'.$amonth.'"')
->whereRaw('ayear = "'.$ayear.'"')
->whereRaw('attendence = 1')
->groupBy('studid')
->toSql();
$absentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS absents_counter'))
->whereRaw('amonth = "'.$amonth.'"')
->whereRaw('ayear = "'.$ayear.'"')
->whereRaw('attendence = 0')
->groupBy('studid')
->toSql();
$stud_attend = DB::table($wys_attendence_table . ' as table')
->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
->where('table.amonth',$amonth)
->where('table.ayear',$ayear)
->groupBy('table.studid')
->get();
var_dump($stud_attend);
试试这个
$attendance = DB::table($wys_attendence_table)
->select(DB::raw('count(attendence) as count_attendance'))
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
编辑
您可以从这两个查询中进行一个查询,并在一个foreach中使用它。
大概是这样的:
$countQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS counter'))
->groupBy('studid')
->toSql();
$stud_attend = DB::table($wys_attendence_table . ' AS table')
->leftJoin(DB::raw('(' . $countQuery . ') AS counter_table'), 'table.studid', '=', 'counter_table.studid')
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
编辑2:
$presentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS presents_counter'))
->whereRaw('attendence = 1')
->groupBy('studid')
->toSql();
$absentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS absents_counter'))
->whereRaw('attendence = 0')
->groupBy('studid')
->toSql();
$stud_attend = DB::table($wys_attendence_table . ' AS table')
->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
->where('amonth',$amonth)
->where('ayear',$ayear)
->groupBy('adate')
->get();
编辑3:
完整代码:
$attendance = DB::table($wys_attendence_table)
->whereRaw('amonth = "'.$amonth.'"')
->whereRaw('ayear = "'.$ayear.'"')
->groupBy('adate')
->get();
$presentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS presents_counter'))
->whereRaw('amonth = "'.$amonth.'"')
->whereRaw('ayear = "'.$ayear.'"')
->whereRaw('attendence = 1')
->groupBy('studid')
->toSql();
$absentsCountQuery = DB::table($wys_attendence_table)
->select(DB::raw('studid, COUNT(*) AS absents_counter'))
->whereRaw('amonth = "'.$amonth.'"')
->whereRaw('ayear = "'.$ayear.'"')
->whereRaw('attendence = 0')
->groupBy('studid')
->toSql();
$stud_attend = DB::table($wys_attendence_table . ' as table')
->leftJoin(DB::raw('(' . $presentsCountQuery . ') AS presents_counter_table'), 'table.studid', '=', 'presents_counter_table.studid')
->leftJoin(DB::raw('(' . $absentsCountQuery . ') AS absents_counter_table'), 'table.studid', '=', 'absents_counter_table.studid')
->where('table.amonth',$amonth)
->where('table.ayear',$ayear)
->groupBy('table.studid')
->get();
var_dump($stud_attend);
先生,我在用这个密码,我出错了。SQLSTATE[42S22]:未找到列:字段列表中的1054个未知列(DB::raw('count('attendence'),其中attendence=1'))(SQL:select
(DB::raw('count('attendence'),其中attendence=1'))
fromwys_8-1_att
WHEREamonth
=07和ayeaar
=2015)在计数中,您要计数的列是什么。列名是Attentience。我想分别计算每个学生的总出席人数。where Attendance=1。我想像上面的框一样。对不起,我不明白->选择((DB::raw('count('column\u you\u want\u count'),其中Attendance=1'))如何写这个“column\u you\u want\u to\u count”?你能给我看一下你数据库的表和列吗。SQLSTATE[42S22]:未找到列:字段列表中的1054个未知列(DB::raw('count('attendence'),其中attendence=1'))(SQL:select(DB::raw('count('attendence'),其中attendence=1'))
fromwys_8-1_att
WHEREamonth
=07和ayeaar
=2015)在计数中,您要计数的列是什么。列名是Attentience。我想分别计算每个学生的总出席人数。where Attendance=1。我想像上面的框一样。对不起,我不明白->选择((DB::raw('count('column\u you\u want\u count'),其中Attendance=1'))如何写这个“column\u you\u want\u to\u count”?先生,你能给我看看你数据库的表和列吗?有什么问题吗?我测试了我的解决方案,效果很好。你有什么经验?试着复制和粘贴我的全部代码,并请测试它。不要只编辑你的部分内容。它必须起作用。我肯定你犯了一些错误。例如,计数器查询应该是toSql()而不是count(),在计数器查询中,必须使用whereRaw而不是where。whereRaw只能得到一个字符串,而不是一个字符串和一个int。因此,如果您不明白为什么不进行编辑,只需复制和粘贴即可。而不是事后想。尝试理解错误消息。如果有什么不好的,请告诉我们。不要只是说“它不起作用了”,谢谢,先生。我试着复制和粘贴我的全部代码并进行测试。i仅显示学生姓名,不显示相应日期的出席人数和缺席人数的值,并在下方选择studid,COUNT()作为出席人数计数器,从wys_8-1_att
中选择amonth=“07”和ayear=“2015”,出席人数=1组,由studid
在下方选择studid,COUNT()作为wys\u 8-1\u att
中的缺席计数器,其中amonth=“07”和ayear=“2015”和attentience=0 group bystudid
…sql查询将打印在我的查看页面上。因此,我可以在代码的帮助下编辑我的代码。我得到了相应日期的正确当前值和缺席值。。最后一个学生的出席人数和缺席人数。问题是什么?我测试了我的解决方案,效果很好。你有什么经验?试着复制和粘贴我的全部代码,并请测试它。不要只编辑你的部分内容。它必须起作用。我肯定你犯了一些错误。例如,计数器查询应该是toSql()而不是count(),在计数器查询中,必须使用whereRaw而不是where。whereRaw只能得到一个字符串,而不是一个字符串和一个int。因此,如果您不明白为什么不进行编辑,只需复制和粘贴即可。而不是事后想。尝试理解错误消息。如果有什么不好的,请告诉我们。不要只是说“它不起作用了”,谢谢,先生。我试着复制和粘贴我的全部代码并进行测试。i仅显示学生姓名,不显示相应日期的出席人数和缺席人数的值,并在下方选择studid,COUNT()作为出席人数计数器,从wys_8-1_att
中选择amonth=“07”和ayear=“2015”,出席人数=1组,由studid
在下方选择studid,COUNT()作为wys\u 8-1\u att
中的缺席计数器,其中amonth=“07”和ayear=“2015”和attentience=0 group bystudid
…sql查询将打印在我的查看页面上。因此,我可以在代码的帮助下编辑我的代码。我得到了相应日期的正确当前值和缺席值。。最后一个学生的出席人数和缺席人数。