mysql count the sum of all rows

med picture med · May 25, 2011 · Viewed 70.8k times · Source

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?

Answer

Devin Burke picture Devin Burke · May 25, 2011

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