Calculate the average of all records in a table and update the column in MYSQL

0

I need to make a Query that passes all records in the table and update the rate_general column (this column is the average of the rate_food + rate_service + rate_price + rate_environment / 4 notes) of each record, how to do this? are 20,000 records.

    
asked by anonymous 03.08.2018 / 20:13

3 answers

0

You can use a procedure that updates the table based on the average of the records, to calculate the records you can use the function avg, I will leave here below two links about procedures and avg: link link

I hope you have helped!

    
03.08.2018 / 22:58
0

Add the average calculation in your update, but if your field is integer type the average will ignore the decimal places, so I'm adding a CAST AS DECIMAL to your average consider field the decimal places.

UPDATE 
   SET rate_general = CAST((rate_food + rate_service + rate_price + rate_environment) AS DECIMAL(12,2)) / 4

Do not forget to add the WHERE clause if you want to add some filter to update the records.

    
03.08.2018 / 20:34
-1

I do not know if it can help, but there is a function in mysql called avg , it does the average of your records

    
03.08.2018 / 20:44