Re: math calculations query

From: Date: Fri, 27 Apr 2001 04:29:51 +0000
Subject: Re: math calculations query
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-50562@lists.php.net to get a copy of this message
"Maxim Maletsky" <maxim.maletsky@japaninc.net> wrote: > SELECT > SUM(quantity)*SUM(total) As mySUM > FROM > table > WHERE Technically, this is correct based on what the original poster (below) stated he wanted, but that's not really what he wanted since there's no real world reason to calculate what he stated he wanted. He must really want the total monetary value of the items returned by the query. That's calculated by multiplying the quantity of each record by the price of each item and summing that for each record. So the correct SQL statement is: SELECT SUM(price*quantity) FROM table_name WHERE ... If he doesn't want to run the query above in addition to his existing query to return individual records, he can calculate this total programatically. Sean, in this case, as you're looping through the records do: $amount_record = $price * $quantity; $amount_total += $amount_record; After the loop is complete, $amount_total will contain the value you want. Alternately, the first line can be eliminated by calculating the amount for each record within the query. sean@networkdata.co.uk [mailto:sean@networkdata.co.uk] wrote: > i have a script which selects description, price and quantity from a mysql > table. > > i have displayed the results on a page in the form of description, price, > quantity but I was wondering how best to go about calculating a total total, > ie. sum of quantitys multiplied by sum of prices. -- Steve Werby President, Befriend Internet Services LLC http://www.befriend.com/

« previous php.general (#50562) next »