MMOCC the isometric world scene, 2006–now
Archive. Archived from the original MMOCC Forum (2006–2010), recovered from the Internet Archive. The forum is read-only history now — the scene talks on Discord. If you posted here and want something removed, get in touch.

php and mysql help?

10 posts · 2012-07-13 · MyBB era
07-13-2012 12:39 AM #1
I have two columns in my database - rating and matches

I have a third column called average

I want to cycle through EVERY row in the database and work out the average of the values rating and matches and place the result in average.

A bit of help? Thanks

07-13-2012 01:39 AM #2
I don't think I really understand your setup.

You've got your table (database) set up like this:
match - rating - average

How do you want to fill that average with only 1 rating? Or is there duplicate rows for 1 match with different ratings, and you want the average of them all to be in all rows of that match? OR does the "rating" field contain multiple ratings?

07-13-2012 09:16 AM #3
The rating field is many ratings added up from multiple "matches." The amount of matches is found in the "matches" column.

Basically before doing some work to the average data I need to make sure the value is correct and up to date. So I need a peice of code that goes through each row in the table and works out the average and places the correct value into "average."

07-13-2012 09:33 AM #4
Code:
rating | matches | average
---------------------------
     1 |       3 |       2?
---------------------------
     0 |      10 |       5?
---------------------------
     3 |       3 |       3?
---------------------------
     5 |      15 |      10?

Am I on the right track with how you want it to work? e.g. the average of 0 and 10 is 5 etc..

07-13-2012 08:21 PM #5
Yes. I know how to do the average calculation but am unsure on how to do a loop which runs through every row in the database and does this average calculation.

07-13-2012 08:28 PM #6
Code:
UPDATE mytable SET average = (rating + matches) / 2

07-14-2012 07:15 AM #7
The average isn't correct like that though. Rating 5 over 15 matches isn't an average of 10? Or is it just wrong naming of your rows..

07-14-2012 11:48 PM #8
Lets say someone has been rated like this:

Match 1: 10
match 2: 8
Match 3: 9
Match 4: 2

There added rating is 29 over 4 games. So their average rating for the season so far is 7.25.

All I need is a little help explaining how I'd create a php loop that would take every row in the database and apply this calculation. I know how to do it for 1 row, but how is it done for every row?

07-15-2012 01:37 AM #9
Check out GROUP BY and AVERAGE().

Recovered from /printthread.php?tid=19182 · captured 2012.