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. Link to comment https://forums.phpfreaks.com/topic/118311-average-per-level-calculation/ 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 Link to comment https://forums.phpfreaks.com/topic/118311-average-per-level-calculation/#findComment-608932 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. Link to comment https://forums.phpfreaks.com/topic/118311-average-per-level-calculation/#findComment-608938 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.