|
-
Jun 16th, 2002, 03:53 AM
#1
Thread Starter
Hyperactive Member
MySQL (TOTAL)
I have this table:
ID SUM
1 2
2 5
3 3
There is any function to give me the total of SUM column:
2+5+3=10
I can calculate them using math such as $i=$i+$row[sum] .. but I ask about function to do that auto.
Thanks...
-
Jun 16th, 2002, 09:00 AM
#2
you could put those into an array and then add them that way using array_sum()
or when you pull them out of the DB you can add them to each other like you are doing.
-
Jun 21st, 2002, 08:51 PM
#3
New Member
I think you can also use:
select sum(columnName) as variable from table;
You can use that query and i think the value will be stored in the variable $variable.
-
Jun 22nd, 2002, 04:01 AM
#4
Fanatic Member
or just run a loop that will add them together. I'll post an example in a moment
-
Jun 22nd, 2002, 04:05 AM
#5
Fanatic Member
wouldn't something like this work:
PHP Code:
$x = 0;
$the_sum = 0;
While ($x < mysql_num_rows($query))
{
$the_sum = $the_sum + mysql_result($query,$x,"sum");
$x++;
}
ECHO "Sum of row: $the_sum";
?
-
Jun 22nd, 2002, 11:03 AM
#6
yup that could work but HeaveNGuY has the right idea. besides using mysql_result is a lot slower then using sum()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|