mysql计算所有行的总和

时间:2022-09-24 00:22:01

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表,并且在每一行中都有一个名为“value”的字段,字段值因行而异。我想要的是选择所有行并计算所有“值”字段的总和。

any idea?

任何想法?

6 个解决方案

#1


20  

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 BY clause:

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

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

#2


4  

SELECT SUM(value) as total FROM table;

$row['total'];

#3


3  

SELECT SUM(`value`) FROM `your_table`

#4


2  

SELECT SUM(value)
    FROM YourTable

#5


1  

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

你想要的是名为SUM的GROUP函数。

#6


0  

This query will return the sum of value and the number of rows count:

此查询将返回值的总和和行数:

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

#1


20  

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 BY clause:

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

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

#2


4  

SELECT SUM(value) as total FROM table;

$row['total'];

#3


3  

SELECT SUM(`value`) FROM `your_table`

#4


2  

SELECT SUM(value)
    FROM YourTable

#5


1  

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

你想要的是名为SUM的GROUP函数。

#6


0  

This query will return the sum of value and the number of rows count:

此查询将返回值的总和和行数:

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