supanoob Posted August 3, 2006 Share Posted August 3, 2006 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, dexand 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? Quote Link to comment Share on other sites More sharing options...
CTM Posted August 3, 2006 Share Posted August 3, 2006 SELECT SUM(speed) as speed_amount, SUM(power) as power_amount, SUM(dex) as dex_amount FROM tableThat'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 Quote Link to comment Share on other sites More sharing options...
supanoob Posted August 3, 2006 Author Share Posted August 3, 2006 and then how would i add them all up? or would that do it for me Quote Link to comment Share on other sites More sharing options...
hostfreak Posted August 3, 2006 Share Posted August 3, 2006 It would add each individual field up, so in the case of the code he posted:speed_amount = 9power_amount = 9dex_amount = 9Now 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); Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 3, 2006 Share Posted August 3, 2006 [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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.