This is a discussion on Re: [PHP] Feeling a bit brain dead, please help in maths: averages within the PHP General forums, part of the PHP Programming Forums category; Hey, Thanks for replying. Actually I made that example simple, i actually need the average of 7 to 9 fields.......
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hey,
Thanks for replying. Actually I made that example simple, i actually need the average of 7 to 9 fields....and i dont want to run 7 selects :-( I know its been done on elance....but how? Thanks, -Ryan > off the top of my head: > > SELECT AVG(age), SUM(salTotal) > FROM blah > WHERE section = 3 > > i'm not sure if you can use the 2 functions in the same query, > you might have to break it into 2 queries. > > Craig > > -----Original Message----- > From: Ryan A [mailto:ryan@coinpass.com] > Sent: Wednesday, September 17, 2003 3:08 PM > To: php-general@lists.php.net > Subject: [php] Feeling a bit brain dead, please help in maths: averages > > > Hey, > Been working a bit too much i guess so am feeling braindead... > > I have a couple of records in the database, i am doing a: "select > age,salTotal from blah where section=3" > this is getting me a list of records, how do i calculate the average age and > salary total (salTotal) to display in my php page? > > Cheers, > -Ryan A > > > > > > > > We will slaughter you all! - The Iraqi (Dis)information ministers site > http://MrSahaf.com > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > |
|
|||
|
> -----Original Message-----
> From: Ryan A [mailto:ryan@coinpass.com] > Sent: Wednesday, September 17, 2003 3:21 PM > To: craig@topdraw.com; php-general@lists.php.net > Subject: Re: [php] Feeling a bit brain dead, please help in maths: > averages > > > Hey, > Thanks for replying. > > Actually I made that example simple, i actually need the average > of 7 to 9 > fields....and i dont want to run 7 selects :-( > I know its been done on elance....but how? > > Thanks, > -Ryan this should do the trick... but it's not tested ;) #assuming you are using mysql and your query #has successfully executed and your results #are in $resultSet $rowCount = 0; $ageSum = 0; $salarySum = 0; #use as many sums as needed while ($row = mysql_fetch_object($resultSet)) { $ageSum += $row->age; $salarySum += $row->sal; $rowCount++; } $salaryAvg = $ageSum / $rowCount; > > > > off the top of my head: > > > > SELECT AVG(age), SUM(salTotal) > > FROM blah > > WHERE section = 3 > > > > i'm not sure if you can use the 2 functions in the same query, > > you might have to break it into 2 queries. > > > > Craig > > > > -----Original Message----- > > From: Ryan A [mailto:ryan@coinpass.com] > > Sent: Wednesday, September 17, 2003 3:08 PM > > To: php-general@lists.php.net > > Subject: [php] Feeling a bit brain dead, please help in maths: averages > > > > > > Hey, > > Been working a bit too much i guess so am feeling braindead... > > > > I have a couple of records in the database, i am doing a: "select > > age,salTotal from blah where section=3" > > this is getting me a list of records, how do i calculate the average age > and > > salary total (salTotal) to display in my php page? > > > > Cheers, > > -Ryan A > > > > > > > > > > > > > > > > We will slaughter you all! - The Iraqi (Dis)information ministers site > > http://MrSahaf.com > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > |
![]() |
| Thread Tools | |
| Display Modes | |
|
|