例子:要得出已经得交试卷(is_finish=0)和未提交试卷(is_finish=1)的总数
//已做试卷总数量 试卷(已提交)
$user_test_total = Db::name('test_paper_user')
->where('user_id',$uid)
->where('is_finish','in','0,1')//是否交卷,0为是,1为否
->where('deleted',0)
->group('is_finish')//查询is_finish=0,is_finish=1的结果放到两个组中保存
->field('count(*) as count,is_finish')//count(*) as count 统计总作为count字段
->select();
dump($user_test_total);
取值方法:
一、打印记录集:$user_test_total 得出数组
array(19) {
["test_total"] => int(0)
["user_test_total"] => array(2) {
[0] => array(2) {
["count"] => int(1)
["is_finish"] => int(0)
}
[1] => array(2) {
["count"] => int(1)
["is_finish"] => int(1)
}
}
二:取值array_column($user_test_total,'count',is_finish);通过下标is_finish得对应count字段的值
$arr1=array_column($user_test_total,'count',is_finish);
dump($arr1);
打印dump($arr1)结果:
array(2) {
[0] => int(1)
[1] => int(1)
}
$is_finish_0=$arr1[0];
$is_finish_1=$arr1[1];
进阶:在分组中再分组
相当在已经得交试卷(is_finish=0)和未提交试卷(is_finish=1)中再分正确、错误、未做分别总数是多少
//已做试题总数量 试题
$list2 = Db::name('test_paper_question_user')
->where('user_id',$uid)
->where('type','in','1,2,3,4,6,7')//试题类型,1为单选题,2为多选题,3为判断题,4简答题,5分析题,6填空题,7不定项选择题
->where('deleted',0)
->group('type,is_correct')//查询type=1,type=2等等的结果放到数组中保存
->field('count(*) as count,type,is_correct')//count(*) as count 统计总作为count字段
->select();
if(!empty($list2)){
foreach($list2 as $v){
if($v['type']==1){//1为单选题
//is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
if($v['is_correct']==0){//正确
$total1_0 += $v['count'];//已做[单选题]-正确试题总数量
}elseif($v['is_correct']==1){//错误
$total1_errnum += $v['count'];//已做[单选题]试题-错题总数量
}else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
$total1_2 += $v['count'];
}
}elseif($v['type']==2){//2为多选题
//is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
if($v['is_correct']==0){//正确
$total2_0 += $v['count'];//已做[单选题]-正确试题总数量
}elseif($v['is_correct']==1){//错误
$total2_errnum += $v['count'];//已做[单选题]试题-错题总数量
}else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
$total2_2 += $v['count'];
}
}elseif($v['type']==3){//3为判断题
//is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
if($v['is_correct']==0){//正确
$total3_0 += $v['count'];//已做[单选题]-正确试题总数量
}elseif($v['is_correct']==1){//错误
$total3_errnum += $v['count'];//已做[单选题]试题-错题总数量
}else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
$total3_2 += $v['count'];
}
}elseif($v['type']==4){//4简答题
//is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
if($v['is_correct']==0){//正确
$total4_0 += $v['count'];//已做[单选题]-正确试题总数量
}elseif($v['is_correct']==1){//错误
$total4_errnum += $v['count'];//已做[单选题]试题-错题总数量
}else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
$total4_2 += $v['count'];
}
// }elseif($v['type']==5){//5分析题
// //is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
// if($v['is_correct']==0){//正确
// $total5_0 += $v['count'];//已做[单选题]-正确试题总数量
// }elseif($v['is_correct']==1){//错误
// $total5_errnum += $v['count'];//已做[单选题]试题-错题总数量
// }else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
// $total5_2 += $v['count'];
// }
}elseif($v['type']==6){//6填空题
//is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
if($v['is_correct']==0){//正确
$total6_0 += $v['count'];//已做[单选题]-正确试题总数量
}elseif($v['is_correct']==1){//错误
$total6_errnum += $v['count'];//已做[单选题]试题-错题总数量
}else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
$total6_2 += $v['count'];
}
}elseif($v['type']==7){//7不定项选择题
//is_correct 答题是否正确,0位正确,1为错误,2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
if($v['is_correct']==0){//正确
$total7_0 += $v['count'];//已做[单选题]-正确试题总数量
}elseif($v['is_correct']==1){//错误
$total7_errnum += $v['count'];//已做[单选题]试题-错题总数量
}else{//2为其他(主观题(简单题)=2是未审核,审核可能变成0或1;客观题(单项题,多项题....)=2是用户没有未做这道题)
$total7_2 += $v['count'];
}
}
}
}
结果:
array(6) {
[0] => array(3) {
["count"] => int(10)
["type"] => int(1)
["is_correct"] => int(0)
}
[1] => array(3) {
["count"] => int(7)
["type"] => int(1)
["is_correct"] => int(1)
}
[2] => array(3) {
["count"] => int(7)
["type"] => int(1)
["is_correct"] => int(2)
}
[3] => array(3) {
["count"] => int(3)
["type"] => int(2)
["is_correct"] => int(1)
}
[4] => array(3) {
["count"] => int(1)
["type"] => int(3)
["is_correct"] => int(0)
}
[5] => array(3) {
["count"] => int(1)
["type"] => int(4)
["is_correct"] => int(1)
}
}
二、进阶版 - 去重复(对某个字段只统计一次)
统计观看直播、点播、回放分别的数据,cc_id2=节ID,一节课无论直播、点播、回放都只统计一次
//统计用户已经上课的次数
$list1 = Db::name('live_user_statistics')
->where('user_id',$uid)
->where('pid','in',$allpidarr)
->where('state',1)//用户观看类型 0=后台人员观看 1=前端学生观看
->group('type')//观看类型 0=观看直播 1=观看直播回放 2观看点播
->field('type,count(distinct cs_id2) as count')//count(*) as count 统计总作为count字段
->select();
dump($list1);
结果:
array(3) {
[0] => array(2) {
["type"] => int(0)
["count"] => int(2)
}
[1] => array(2) {
["type"] => int(1)
["count"] => int(1)
}
[2] => array(2) {
["type"] => int(2)
["count"] => int(1)
}
}