mysql - 两个触发器,更新表时出现错误

标签 mysql sql triggers

我得到了以下两个触发器,每个触发器都单独工作,但在一起时却不起作用。

如何让它们协同工作?他们更新表中的不同字段。

触发器 1:

create trigger wys_sk_u after update on `things`
for each row
begin
UPDATE `current` s 
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id= s.id
SET s.`curr_cash` = u.cash1 * b.cash2/ 100;
end;
$$

触发器 2:

create trigger suma_u after update on `current`
for each row
begin
UPDATE `current`
SET `mysum` = `curr_cash` + `mysum`;
end;
$$

第一个应该在 cash1cash2 更新时更新,并更改 curr_cash 的值。 第二个应该在 curr_cash 更新时更新,并更改 mysum

当我编辑表格内容时出现以下错误:

#1442 - Can't update table 'current' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

@编辑 为问题添加了新的答案。


如果我想做这样的事情怎么办:

CREATE TRIGGER total BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
if new.`new_pay_date` <> old.`new_pay_date`
  SET new.`total_cash` = new.`curr_cash` + new.`total_cash`;
end if;
END;
$$

错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET new.`total_cash` = new.`curr_cash` + new.`total_cash`; end if;' at line 4

没有

if new.`new_pay_date` <> old.`new_pay_date`
end if;

但我需要检查这个,并且只更新日期更改。

当前表:

curr_cash
new_pay_date
id_person
id_thing
total_cash

谁能帮我解决这个问题?

最佳答案

问题出在第二个触发器中。尝试使用 BEFORE UPDATE 触发器使用 SET 语句更改字段值 -

CREATE TRIGGER suma_u BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
  SET new.`mysum` = new.`curr_cash` + new.`mysum`;
END;

关于mysql - 两个触发器,更新表时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684202/

相关文章:

Mysql 一张表中有多个订单行

php - 具有上下文的基于角色的访问控制(RBAC)?

SQL select 语句 union all with itself

C# SQL 导出格式

sql-server - SQL Server 当查询结果不为空时发送电子邮件

php - 选择然后更新计数 mysqli

jquery - 如何参数化sql查询

c# - 每当通过 Web 应用程序将记录插入 SQL Server 时,触发 winforms 程序运行

javascript - 带有非动态输入字段的动态输入字段插入到两个 mysql 表中不起作用

mysql - 如何在触发器中使用具有动态字段名称的变量作为 ON DUPLICATE KEY (MariaDB/MySQL)