0

従業員の給与を更新する必要がある新しいトリガーを作成しました。テーブル支払いから2つの値(現金と月)を受け取り、それらを「how_much_to_pay」列に分割します。

トピックを見ました: MySQLトリガーは、トリガーが割り当てられている同じテーブルの行を更新できません。推奨される回避策は?

しかし、1つのテーブルからしか使用せず、問題なく「新しい」ものを配置できるため、役に立ちませんでした。

これが私のトリガーです:

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

2つのテーブル間に1対1の関係がある場合は、BEFOREINSERTトリガーでこのコードを使用できます-

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 に答える