Mysql不通过触发器更新同表不同列的avg函数

标签 mysql sql

我正在尝试更新特定列 column_2column_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.movi​​ng_avg

关于Mysql不通过触发器更新同表不同列的avg函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45308969/

相关文章:

php - 如何在codeigniter中查找重复记录

java - MySQL-Server/Apache Tomcat 和使用 Hibernate/JPA 的 Tapestry 项目的组合使用什么配置?

mysql - 搜索查询 mySQL 的排序结果

PHP 检查空数组表单输入

java - 按父对象设置的子对象的值对父对象进行排序

mysql - 将列从一个表复制到另一个表

sql - 使用 Group by 的 SQL 命令出错?

mysql - 在此 SQL 查询中 Tmp(email) 有什么用?

mysql - 如何从 UNION ALL 之外的另一个表获取列值?

mysql - 错误 1452 : Cannot add or update a child row: a foreign key constraint fails