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? Link to comment https://forums.phpfreaks.com/topic/16492-adding-field-together/ 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 Link to comment https://forums.phpfreaks.com/topic/16492-adding-field-together/#findComment-68817 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 Link to comment https://forums.phpfreaks.com/topic/16492-adding-field-together/#findComment-68859 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); Link to comment https://forums.phpfreaks.com/topic/16492-adding-field-together/#findComment-68863 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] Link to comment https://forums.phpfreaks.com/topic/16492-adding-field-together/#findComment-68865 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.