mysql 统计所有行的总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6131197/
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-31 20:01:34  来源:igfitidea点击:

mysql count the sum of all rows

mysqlsqldatabaseaggregate-functions

提问by med

I have a mysql table that has a number of rows, and in each row a field called "value", the field value will differ from row to row. What I want, is to select all the rows and count the sum of all the "value" fields.

我有一个包含多行的 mysql 表,每行中有一个名为“值”的字段,该字段值会因行而异。我想要的是选择所有行并计算所有“值”字段的总和。

any idea?

任何的想法?

回答by Devin Burke

Do you mean like this?

你的意思是这样吗?

SELECT    SUM(value)
FROM      myTable

If you have multiple columns to return, simply add each non-aggregate (i.e., summed) row to the GROUP BYclause:

如果您有多个要返回的列,只需将每个非聚合(即求和)行添加到GROUP BY子句中:

SELECT    firstName, lastName, SUM(value)
FROM      myTable
GROUP BY  firstName, lastName

回答by Lawrence Cherone

SELECT SUM(value) as total FROM table;

$row['total'];

回答by Tomas Markauskas

SELECT SUM(`value`) FROM `your_table`

回答by Joe Stefanelli

SELECT SUM(value)
    FROM YourTable

回答by Lukas Knuth

What you'll want is the GROUP-function named SUM.

您需要的是GROUP名为SUM的函数。

回答by Amresh

This query will return the sum of valueand the number of rows count:

此查询将返回总和value和行数计数:

SELECT count(*), sum(value) FROM tablename