我正在尝试更新特定列 column_2
取 column_1
行的平均值值其中 column_3 =
值
但它给出了一个错误
我的代码
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;
最佳答案
根据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.
我宁愿编写一个 BEFORE INSERT
触发器,并使用现有行的平均值和新的 price
更新 :New.moving_avg
。
关于Mysql不通过触发器更新同表不同列的avg函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45308969/