Jump to content

Archived

This topic is now archived and is closed to further replies.

supanoob

Adding field together

Recommended Posts

well i want to add multiple fields together without having to list them all.

for example say i have 3 different things i want adding together:

speed, power, dex

and i have 3 people with these in the db.. now i want to add all three speed together, all 3 power and all 3 dex.

how would i do it?

Share this post


Link to post
Share on other sites
SELECT SUM(speed) as speed_amount, SUM(power) as power_amount, SUM(dex) as dex_amount FROM table

That's assuming you'd want to select the sum of each attribute for every user in table "table". This is a mysql query by the way :p

Share this post


Link to post
Share on other sites
It would add each individual field up, so in the case of the code he posted:
speed_amount = 9
power_amount = 9
dex_amount = 9

Now are you talking about adding the sum of each of those up as well? To get 27? If so, Im not sure how to do it by sql but you could then do this:

print($speed_amount + $power_amount + $dex_amount);

Share this post


Link to post
Share on other sites
[code]
<?php
$query="SELECT SUM(speed) as speed_amount, SUM(power) as power_amount, SUM(dex) as dex_amount FROM table";
while($record=mysql_fetch_assoc($query)) {

$total_result=$record['speed_amount']+$record['power_amount']+$record['dex_amount'];

echo"Totlal for speed is: $record['speed_amount']<br>
Totlal for power is: $record['power_amount']<br>
Totlal for dex is: $record['dex_amount']<br>
Totlal is: $total_result<br>";
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.