Kai
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
Xikeon
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?
Kai
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."
xLite
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..
Kai
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.
xLite
07-13-2012 08:28 PM
#6
Code:
UPDATE mytable SET average = (rating + matches) / 2
Xikeon
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..
Kai
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?
xLite
07-15-2012 01:37 AM
#9
Check out GROUP BY and AVERAGE().