php数据表求和 php数组求和函数
在php中怎样将数据库中文本型数据求和
可以直接使用mysql的sum函数求和,mysql会自动转换为数字型进行求和.
成都创新互联公司是一家以重庆网站建设公司、网页设计、品牌设计、软件运维、seo优化排名、小程序App开发等移动开发为一体互联网公司。已累计为成都食品包装袋等众行业中小客户提供优质的互联网建站和软件开发服务。
如何在PHP中显示数据库中的求和函数计算出的值
php+mysql,
举个例子
...........省略数据库连接3步骤
$sql
=
"select
sum(price)
from
table";
//查询price字段下所有值的和
$query
=
mysql_query($sql);
//执行sql语句
$result
=
mysql_fetch_row($query);
//取得搜索结果中的数据(一个数组)
echo
$result[0];
//
数组中只有一条数据,所以索引
就可以了
echo就是显示啦,
希望能帮到你
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表中如何求递归求和汇总?
function sumShuzi($tree, $updateData = array()) {
$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数据表求和 php数组求和函数
本文网址:http://azwzsj.com/article/hpdcpc.html