Reputation: 1215
As an example I will have this table from MySQL
Id | Name | Grade 1 | Grade 2 | Average
1. | Jack | 9 | 10 |
2. | Jimmy | 9 | 8 |
2. | Emmy | 9 | 7 |
So, in the Average field from this table, I need to calculate the AVERAGE from the Grade 1 and Grade 2 fields. I tried a lot of possiblities which I know they are wrong like:
UPDATE table_name SET Average=AVG(Grade 1 + Grade 2)
I there a way to do this? Can anyone help me? Thanks!
Upvotes: 1
Views: 2848
Reputation: 15048
You need to add the fields together and divide by the number of fields. If your Average
field is of DECIMAL
type you don't really even need to specify the ROUND
function. Any decimal exceeding the declaration will just be truncated (SQL Fiddle) :
UPDATE table_name
SET AVERAGE = (grade1 + grade2) / 2;
In your example you only have two fields that you are getting the average of. So Average decimal(3,1)
would work for you since the most the decimal portion will ever be is .5
. So the ROUND
function is clearly not needed.
Upvotes: 1