Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql count the sum of all rows

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.

any idea?

like image 883
med Avatar asked May 25 '11 21:05

med


People also ask

How do I sum all rows in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.

How count all rows in MySQL table?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

How do I average a count in MySQL?

MySQL AVG() function retrieves the average value of a given expression. If the function does not find a matching row, it returns NULL. Where expr is a given expression. The DISTINCT option can be used to return the average of the distinct values of expr.


2 Answers

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:

SELECT    firstName, lastName, SUM(value)
FROM      myTable
GROUP BY  firstName, lastName
like image 82
Devin Burke Avatar answered Oct 05 '22 22:10

Devin Burke


SELECT SUM(`value`) FROM `your_table`
like image 43
Tomas Markauskas Avatar answered Oct 06 '22 00:10

Tomas Markauskas