[英]Mysql not updating the avg function of same table different column by trigger
i am trying to update a specific column that is column_2
by taking the average of rows of column_1
values where column_3 =
value 我正在尝试通过获取
column_1
值的行的平均值来更新特定列column_2
,其中column_3 =
值
but it gives a error 但它给出了一个错误
My code 我的密码
Error: Can't update table 'moving_average' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
delimiter $$
Create TRIGGER takeaverage AFTER insert ON moving_table
FOR EACH ROW
BEGIN
update moving_table b, (select avg(price) avg_ from moving_table ) v
set
b.moving_avg = v.avg_price;
END;
You can't do that, as per the documentaion : 根据文档 ,您不能这样做:
A stored function or trigger cannot modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
存储的函数或触发器无法修改调用该函数或触发器的语句已在使用(用于读取或写入)的表。
I would rather write a BEFORE INSERT
trigger and update the :New.moving_avg
with the average of existing rows and the new price
. 我宁愿编写一个
BEFORE INSERT
触发器,并用现有行和新price
的平均值更新:New.moving_avg
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.