Jump to content

Adding field together


supanoob

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?
Link to comment
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
Link to comment
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);
Link to comment
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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.