Jump to content

Average per level calculation


EchoFool

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.