0

我做了新的触发器,它必须更新员工的工资。它从表支付中获取 2 个值(现金和月份),并将它们分成“how_much_to_pay”列。

我查看了主题: MySQL 触发器无法更新触发器分配到的同一个表中的行。建议的解决方法?

但这对我没有帮助,因为他们只使用一张桌子,他们可以毫无问题地放入“新”。

这是我的触发器:

create trigger pay_out before insert on `payment_out`
for each row
then
UPDATE `payment_out` o
INNER JOIN payment p ON p.id_1 = o.id_1 AND o.id2 = p.id2
SET o.`how_much_to_pay` = p.cash / p.months;
end;
$$

以下是表格:

table payment_out
id1
id2
how_much_to_pay

table payment
id1
id2
cash
months

和错误本身:

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

1 回答 1

1

如果两个表之间存在一对一关系,则可以在 BEFORE INSERT 触发器中使用此代码 -

BEGIN
  SELECT p.cash, p.months INTO @cash, @months
    FROM payment p
    WHERE p.id_1 = NEW.id_1 AND p.id2 = NEW.id2;

  SET NEW.how_much_to_pay = @cash / @months;
END
于 2012-12-04T09:35:36.517 回答