How can I get the sum of all the values in a column returned by my query?
$query['data'];
How do I add up all the values? In the column "data"?
I can think of doing it in a loop while mysql_fetch_assoc.
Or by grabbing mysql_num_rows and going through the array one value at a time.
But is there a simpler/cleaner way of doing it all?
Thanks
Ron
What is easiest way to add up all values returned by query?
Moderator: General Moderators
-
bionicdonkey
- Forum Contributor
- Posts: 132
- Joined: Fri Jan 31, 2003 2:28 am
- Location: Sydney, Australia
- Contact:
you can do it within the query..i think it's something like:
Code: Select all
SELECT SUM(tablename.fieldname) AS whatever
FROM tablename
GROUP BY fieldname;Code: Select all
select count(fieldname) from tablename where ...The query with the SUM (...) statement will return the sum of all columns. I don't know the exact mySQL syntax for it, but it will take you in the right direction
-
bionicdonkey
- Forum Contributor
- Posts: 132
- Joined: Fri Jan 31, 2003 2:28 am
- Location: Sydney, Australia
- Contact:
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Which is why info about it is located here:bionicdonkey wrote:i'm preety sure SUM() comes under the GROUP BY clause
http://www.mysql.com/doc/en/Group_by_functions.html
Mac