php访问数据库求和,mysql数据库求和
php与mysql表中如何求递归求和汇总?
function sumShuzi($tree, $updateData = array()) {
创新互联专业成都网站设计、网站建设,集网站策划、网站设计、网站制作于一体,网站seo、网站优化、网站营销、软文发稿等专业人才根据搜索规律编程设计,让网站在运行后,在搜索中有好的表现,专业设计制作为您带来效益的网站!让网站建设为您创造效益。
$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输出sql求和结果问题
楼上的已经说的很清楚,如果还不会,我就把全部代码写出来吧:
sql="select sum(jifen) as sumjifen,card from jifen group by card";
$result=mysql_query($sql,$myconn) or die(mysql_error());
while($row=mysql_fetch_array($result)){
echo $row["card"]." ".$row["sumjifen"]."br";
}
php查询mysql的数值结果怎么相加?
select sum("金额字段") as cmoney from 表 where 1;\x0d\x0a\x0d\x0a然后你输出cmoney这个就是相加的效果。\x0d\x0a还有一种比较差点的方法就是:\x0d\x0a 1、select 金额字段 from 表 where 1;\x0d\x0a2、获取你得到的所有信息,然后foreach 把所有的金额相加就行,\x0d\x0a 实例:$sql = "select money from cg_money where 1";\x0d\x0a $query = mysql_query($sql);\x0d\x0a$m = 0;\x0d\x0a\x0d\x0awhile($rs = mysql_fetch_array($sql)){\x0d\x0a $m = $m+$rs['money'];\x0d\x0a\x0d\x0a}\x0d\x0aecho $m;这个也是可以获取所有的money的,就是费事了点,还是上面的呢个方法好滴,也就是楼上的呢个方法
php 数据库 求和
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 行受影响)
php查询mysql的数值结果怎么相加
select sum("金额字段") as cmoney from 表 where 1;
然后你输出cmoney这个就是相加的效果。
还有一种比较差点的方法就是:
1、select 金额字段 from 表 where 1;
2、获取你得到的所有信息,然后foreach 把所有的金额相加就行,
实例:$sql = "select money from cg_money where 1";
$query = mysql_query($sql);
$m = 0;
while($rs = mysql_fetch_array($sql)){
$m = $m+$rs['money'];
}
echo $m;这个也是可以获取所有的money的,就是费事了点,还是上面的呢个方法好滴,也就是楼上的呢个方法
分享题目:php访问数据库求和,mysql数据库求和
URL地址:http://scyanting.com/article/phsdis.html