欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

关于php处理表格统计功能

程序员文章站 2022-06-05 23:43:38
...
我做一个统计页面效果是

所建立的数据库表是

求解怎么用php实现,谢谢!


回复讨论(解决方案)

一条 SQL 指令就可完成

@xuzuning
大神。。。。。。一条怕是不行吧!

确实一条可以,按部就班一个个项加进去

我是菜鸟啊!完全使不上劲!

select 班级, count(*) as 人数, sum(分数) as 总分数, sum(if(成绩是否有效='是',1,0) as 有效人数, sum(if(成绩是否有效='是',分数,0) as 有效成绩 ........

不写了,你偷懒用截图,我也不想打字

@xuzuning sum(if(成绩是否有效='是',1,0))这个算不出成绩有效人数啊

用group by 和having试试

@xuzuning @jmy454157571

sql是对了,可是Php循环输出不了
$mysqli=new mysqli("localhost","root","","car_insure");
$mysqli->set_charset("utf8");
if($mysqli->connect_error){
die("连接失败".$mysqli->conect_error);
}
$sql="select t3.*,t4.totalNum as '无效总人数',t4.totalScore as '无效总分数' from
(select t1.class_ as '班级',t1.totalNum as '总人数',t1.totalScore as '总得分',t2.totalNum as '有效总人数',t2.totalScore as '有效总得分' from (select class_,count(id_) as totalNum,sum(score_) as totalScore from score group by class_) as t1 left join (select class_,count(id_) as totalNum,sum(score_) as totalScore from score group by class_,flag_ having flag_='是') as t2 on t1.class_=t2.class_)
as t3
left join (select class_,count(id_) as totalNum,sum(score_) as totalScore from score group by class_,flag_ having flag_='否') as t4
on t3.`班级`=t4.class_"
$res=$mysqli->query($sql);

if($res){
echo"2222";
}else{
echo"1111";
}
$mysqli->close();
?>
输出是1111
这是怎么回事儿,$res没取到值啊!麻烦看下哪里的问题

sql语句在Mysql里面查询出来是对的。