在一行中一起计算所有字段


Calculate all fields together in a row

如何在一段时间内计算所有字段?示例:我有一段时间,它们有四行。每一行都有一个特定的数字。我想要的是,相同的数字总数是多少?

while ($row= mysql_fetch_assoc($select)) {
echo $row['persons'];
}

是指一个群体中有多少人。我希望四排的所有人都像这样。

$row['persons'](5) + 
$row['persons'](6) +
$row['persons'](3) +
$row['persons'](3) = 17

(x) 表示现场有多少人。我怎么能在一段时间内计算这个,所以我的echo是:只有 17。

非常基本的问题,例如:

$total = 0
while ($row= mysql_fetch_assoc($select)) {
   $total += $row['persons'];
}
echo $total;

请尝试此代码请使用 SUM 函数

$result = mysql_query("SELECT SUM(persons) AS personCount  FROM tableName");

if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;

}

$row = mysql_fetch_row($result);

echo $row[0];

如果我猜对了,你想把persons列中的所有数字相加。

$count = 0;
while ($row= mysql_fetch_assoc($select)) {
    $count += $row['persons'];
}
echo $count;

不过,更好的方法是使用MySQL SUM()聚合内置函数。