扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
function sumShuzi($tree, $updateData = array()) {
创新互联于2013年成立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站、成都外贸网站建设公司网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元礼县做网站,已为上家服务,为礼县各地企业和个人服务,联系电话:028-86922220
$sum = 0;
// foreach($tree as $key = $item) { //这句话有毒
foreach($tree as $key = $item) {
if(isset($item['children'])) {
$oldPshuzi = $tree[$key]['Pshuzi'];
$tree[$key]['Pshuzi'] = sumShuzi($item['children'], $updateData);
if($oldPshuzi != $tree[$key]['Pshuzi']) {
$updateData[$item['id']] = array($tree[$key]['Pshuzi'], $tree[$key]['Pname_ch']);
}
}
$sum += $tree[$key]['Pshuzi'];
}
return $sum;
}
$tree = json_decode('[{"id":"1","Pid":"0","Pname_ch":"\u6e20\u9053\u90e8","Pshuzi":"1638000","children":[{"id":"4","Pid":"1","Pname_ch":"\u9500\u552e\u4e8c\u90e8","Pshuzi":"895000","children":[{"id":"13","Pid":"4","Pname_ch":"\u5468\u7ecf\u7406","Pshuzi":"28","children":[{"id":"28","Pid":"13","Pname_ch":"\u6e56\u5357","Pshuzi":"158000"},{"id":"35","Pid":"13","Pname_ch":"\u65b0\u7586","Pshuzi":"19000"}]},{"id":"40","Pid":"4","Pname_ch":"\u9648\u7ecf\u7406","Pshuzi":"5000"}]}]}]', true);
//$tree是具有父子关系的数据树
sumShuzi($tree, $updateData);
foreach ($updateData as $id = $item) {
$sql = "update your_table set Pshuzi={$item[0]} where id={$id}";
mysqli_query($db, $sql); //$db是你的数据库连接结果
}
那你需要再PHP里面就计算好结果,生成excel文件时候填充数据,导出。
循环之前设置几个变量为0
在循环时
让变量加上列的值
循环完毕后 这个变量的值就是列的合计
$sql='select*from game_zhudan';
//改成
$sql='select *, sum(g_win) as g_win_num, sum(g_jiner) as g_jiner_num from game_zhudan';
SELECT SUM(分数) FROM 表a WHERE 性别='男'
SELECT SUM(分数) FROM 表a WHERE 性别='女'
----测试
CREATE TABLE MASK_TEST(
ID INT,
姓名 varchar(20),
性别 varchar(20),
年龄 int,
分数 int
)
INSERT INTO MASK_TEST VALUES(11,'ZHANG SAN','男',20,88)
INSERT INTO MASK_TEST VALUES(11,'LIU SHAN','女',20,83)
INSERT INTO MASK_TEST VALUES(11,'WANG SAN','男',20,87)
INSERT INTO MASK_TEST VALUES(11,'ZHANG TING','女',20,83)
INSERT INTO MASK_TEST VALUES(11,'ZHAO YAN','女',20,88)
INSERT INTO MASK_TEST VALUES(11,'ZHOU HU','男',20,78)
INSERT INTO MASK_TEST VALUES(11,'WANG QIANG','男',20,98)
INSERT INTO MASK_TEST VALUES(11,'LI QIAN','女',21,68)
SELECT SUM(分数) FROM MASK_TEST WHERE 性别='男'
SELECT SUM(分数) FROM MASK_TEST WHERE 性别='女'
----结果:
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
(1 行受影响)
-----------
351
(1 行受影响)
-----------
322
(1 行受影响)
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流