J4B Posted March 19, 2009 Share Posted March 19, 2009 So I'm looking for the best way to average in a rating someone gives a movie. For example: The Matrix Person 1 - Rates it 5 Person 2 - Rates it 3 Person 3 - Rates it 5 Person 4 - Rates it 4 Person 5 - Rates it 4 So the average is 4.2 but they dont all submit their rating at the same time for me to just average. So would I just have 2 columns in my movies database and have it as follows: rating(A)-------num of ratings(B) so then like follows: (A*B+5)/(B+1)---(0*0+5)/(0+1) (A*B+3)/(B+1)---(5*1+3)/(1+1) (A*B+5)/(B+1)---(4*2+5)/(2+1) (A*B+4)/(B+1)---(4.33*3+4)/(3+1) (A*B+4)/(B+1)---(4.25*4+4)/(4+1) 4.2 But how would I write that properly? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 19, 2009 Share Posted March 19, 2009 can't you just do a total score and a number of scores. then rating = total score / number of scores so for each score, it would be: $item = 123;//Id of the item rating $score = 4;//This is the score that was just submitted UPDATE ratings SET total_score = total_score + $score, num_scores = num_score + 1 WHERE item_id = $item and then to getting the score: SELECT total_score/num_score as rating WHERE item_id = $item Quote Link to comment Share on other sites More sharing options...
J4B Posted March 19, 2009 Author Share Posted March 19, 2009 Thank you! I knew there had to be an easier way I was overlooking. 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.