PHPology is a collective of highly skilled, award winning, web gurus.
Contact Raj on 07985 467 213 or email [email protected]

MYSQL - average rating value

The below MYSQL query came into use for me as a sort of "house keeping" script where I had a 2 tables, products and product_reviews and although product_reviews stored the rating (out of 5) that a user would have entered, I wanted to keep an average rating value within the products table so I did not have to calculate it during my select statement.

 

update tbl_products p set p.review_rating = (select round(avg(p2.rating)) from tbl_products_reviews pr where pr.product_id = p.product_id group by pr.product_id)