取一个属性从1个表到另一个表的平均值

I have 2 tables named product and review. Product table has the following columns -

prod_name(primary key), xyz, rating

And Review table has the columns -

prod_name, username, rating, review

Now whenever a user enters a review and rating, it is added in the review table. I want the average of the rating for that particular product to be updated in product table's rating column.

Example - in product table there is a product "x" and that product has been given a rating 7,8,6 by three users "a","b","c" respectively. and i want the ratings attribute of "x" to be updated to 7 as soon as the three ratings are given.

How do I achieve this?

onclick event of rating you want to do 3 tasks.

1.Insert the new rating value in table Review table.

2.Select the total number of rating for the same product. (you can get the totals of all ratings for the relevant product id and divide in to number of ratings available for the product)

3.Update the product table with the total average rating.