我有一个mysql表,其中包含一个名为'id','name'和'number'的字段。
每一行的字段“数字”都有一个数字。
id name number
1 test 30
2 test2 40
3 lala 23
total = 93
如何计算(加)所有行的编号?
$row['number']+$row['number']+$row['number'] = .. an other number.
答案 0 :(得分:1)
SELECT name, SUM(number) FROM table GROUP BY name
我假设你想要一个名字的总和。
SELECT SUM(number) FROM table
没有分组。
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_sum了解更多信息
答案 1 :(得分:1)
尝试做thuis
select number ,sum(number) as total from table;
然后做
$row['total'] // and it gives u the total sum
答案 2 :(得分:1)
在PHP级别:
$cnt = 0;
while($row = mysql_fetch_assoc($result)) {
$cnt += $row['yourfield'];
}
在MySQL级别:
SELECT SUM(yourfield)
FROM yourtable
GROUP BY yourfield
答案 3 :(得分:0)
您可以像这样查询表格:
select sum(number) from table;
并让mysql为你计算。
答案 4 :(得分:0)
试试这个
SELECT SUM(number) AS total FROM table;