在 PHP 中获取 MySQL 列的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5808522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:33:12  来源:igfitidea点击:

Get sum of MySQL column in PHP

phpmysql

提问by Hyman

I have a column in a table that I would like to add up and return the sum. I have a loop, but it's not working.

我想将表中的一列相加并返回总和。我有一个循环,但它不起作用。

while ($row = mysql_fetch_assoc($result)){
    $sum += $row['Value'];
}

echo $sum;

回答by Flinsch

You can completely handle it in the MySQL query:

完全可以在 MySQL 查询中处理:

SELECT SUM(column_name) FROM table_name;

In the PHP code, try this:

在 PHP 代码中,试试这个:

$result = mysql_query('SELECT SUM(value) AS value_sum FROM codes'); 
$row = mysql_fetch_assoc($result); 
$sum = $row['value_sum'];

Using PDO (mysql_queryis deprecated)

使用 PDO(mysql_query已弃用)

$stmt = $handler->prepare("SELECT SUM(value) AS value_sum FROM codes");
$stmt->execute();

$row = $stmt->fetchAll(PDO::FETCH_OBJ);
$sum = $row->value_sum;

回答by Alex

$query = "SELECT * FROM tableName";
$query_run = mysql_query($query);

$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
    $qty += $num['ColumnName'];
}
echo $qty;

回答by user3314173

Try this:

尝试这个:

$sql = mysql_query("SELECT SUM(Value) as total FROM Codes");
$row = mysql_fetch_array($sql);
$sum = $row['total'];

回答by Mike Q

MySQL 5.6 (LAMP) . column_value is the column you want to add up. table_name is the table.

MySQL 5.6(灯)。column_value 是要相加的列。table_name 是表。

Method #1

方法#1

$qry = "SELECT column_value AS count
        FROM table_name ";

$res = $db->query($qry);

$total = 0;
while ($rec = $db->fetchAssoc($res)) {
    $total += $rec['count'];
}
echo "Total: " . $total . "\n";

Method #2

方法#2

$qry = "SELECT SUM(column_value) AS count 
        FROM table_name ";

$res = $db->query($qry);

$total = 0;
$rec = $db->fetchAssoc($res);
$total = $rec['count'];

echo "Total: " . $total . "\n";

Method #3 -SQLi

方法#3 -SQLi

$qry = "SELECT SUM(column_value) AS count 
        FROM table_name ";

$res = $conn->query($sql);

$total = 0;
$rec = row = $res->fetch_assoc();
$total = $rec['count'];

echo "Total: " . $total . "\n";

Method #4: Depreciated (don't use)

方法#4:折旧(不要使用)

$res = mysql_query('SELECT SUM(column_value) AS count FROM table_name'); 
$row = mysql_fetch_assoc($res); 
$sum = $row['count'];

回答by SagarPPanchal

I have replace your Code and it works well

我已经替换了您的代码并且运行良好

$sum=0;
while ($row = mysql_fetch_assoc($result)){
    $value = $row['Value'];

    $sum += $value;
}

echo $sum;

回答by Kurpus

$result=mysql_query("SELECT SUM(column) AS total_value FROM table name WHERE column='value'");
$result=mysql_result($result,0,0);

回答by Rocket Hazmat

$row['Value']is probably a string. Try using intval($row['Value']).

$row['Value']可能是一个字符串。尝试使用intval($row['Value']).

Also, make sure you set $sum = 0before the loop.

另外,请确保$sum = 0在循环之前进行设置。

Or, better yet, add SUM(Value) AS Val_Sumto your SQL query.

或者,更好的是,添加SUM(Value) AS Val_Sum到您的 SQL 查询中。

回答by Samuel

$sql = "SELECT SUM(Value) FROM Codes";

$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

    sum = $row['SUM(price)'];

}

echo sum;

回答by Harunduet

Here is an example : See the image for all field name at Database/Table. enter image description here

这是一个示例:请参阅数据库/表中所有字段名称的图像。 在此处输入图片说明

we want to add all the values of column "duration_sec" for the date '09-10-2018' and only status 'off'

我们想为日期“09-10-2018”添加“duration_sec”列的所有值,并且只添加状态“关闭”

Below is the sql query:

下面是sql查询:

$sql_qry="SELECT SUM(duration_sec) AS count FROM tbl_npt WHERE date='09-10-2018' AND status='off'";

$duration = $connection->query($sql_qry);
while($record = $duration->fetch_array()){
    $total = $record['count'];
}

echo $total

回答by senthilkumar

Get Sum Of particular row value using PHP MYSQL

使用 PHP MYSQL 获取特定行值的总和

"SELECT SUM(filed_name) from table_name"