EchoFool Posted August 5, 2008 Share Posted August 5, 2008 I am having difficult on the logic of how to make a query to count the average money each level of users have... so the output would look like: Level 1 : Average = £5.00 Level 2 : Averag = £4.50 etc So far i have got to : <?php $Get = mysql_query("SELECT Level,Money FROM users GROUP BY Level ORDER BY Level ASC") Or die(mysql_error()); $Total = mysql_num_rows($Get); While($row = mysql_fetch_assoc($Get)){ $Level = $row['Level']; $Money = $Money + $row['MoneyInHand']; $MoneyAvg = $Money / $Total; Echo 'Level '.$Level.' average money '.$MoneyAvg; Echo '<br><br>'; } ?> But have realized it is totally wrong ! So am kinda stuck. Please help. Quote Link to comment Share on other sites More sharing options...
adam84 Posted August 5, 2008 Share Posted August 5, 2008 Use the AVG function to find the average Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 5, 2008 Share Posted August 5, 2008 that is, perform a query that looks like this: SELECT Level, AVG(Money) AS average_money FROM users GROUP BY Level ORDER BY Level MySQL often has functions that take the work out of your PHP code, you should have a look at that chapter in the MySQL manual. 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.