Jump to content


Photo

Adding field together


  • Please log in to reply
4 replies to this topic

#1 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 03 August 2006 - 08:11 PM

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?

#2 CTM

CTM
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 03 August 2006 - 08:18 PM

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

#3 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 03 August 2006 - 09:56 PM

and then how would i add them all up? or would that do it for me

#4 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 03 August 2006 - 10:00 PM

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);

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 03 August 2006 - 10:08 PM

<?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>";
}


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users