更新时间:2022-12-09 23:08:45
您需要将字段加在一起并除以字段数.如果您的Average
字段是DECIMAL
类型,则实际上甚至不需要指定ROUND
函数.超出声明的任何十进制都将被截断( SQL小提琴 ):
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;
在您的示例中,您只有两个要获取平均值的字段.所以Average decimal(3,1)
将为您工作,因为最多的小数部分是.5
.因此,显然不需要ROUND
功能.
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.