0

I have a column of data returned from my database and I'd like to know how to get the sum for the entire column. Can someone tell me if there is a built in function for this?

123.00
12.00
1.00
-----
136.00 < this is what I need.

Please see the accompanying post for the mysql portion of my code. How to get the total sum for a column

Community
  • 1
  • 1
Jim
  • 33
  • 5
  • No, I'm returning several columns of data and need to get the sum of the column. I can get the total of the row but that isn't what I need. I need the column total. – Jim Jan 26 '11 at 08:25

3 Answers3

3

You can use the aggregate functions of your database SQL; something like SELECT SUM(column) FROM table;. This way you leave the data on the database server, saving bandwidth and php-computational resources.

xtofl
  • 40,723
  • 12
  • 105
  • 192
  • I can't. I'm already returning numerous columns of data and there is no way, that I know of to sum the columns. Please have a look at the other question I posted in the mysql section. – Jim Jan 26 '11 at 08:23
1

in mysql you can return all this values in a sum() function, If important to you also all values and the total amount

if you store this in array in php u can use array_sum

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
1

What do you mean ? if you want to get sum through query

Go for SUM() in query using GROUP BY

SELECT SUM(`price`) FROM TABLE GROUP BY `item`;

or

use

array_sum() in php
Harish
  • 2,311
  • 4
  • 23
  • 28