0

MySQL でこのトリガーを作成しました (テーブル product の更新後):

BEGIN
 set @parentid := (select parent_id from product where product_id = new.product_id);
 if (old.price <> new.price) then
  update product set price=new.price where product_id = @parentid and price > new.price;
 end if;
END

しかし、価格を更新すると、このエラーが発生します

: SQL エラー (1442) このストアド ファンクション/トリガーを呼び出したステートメントで既に使用されているため、ストアド ファンクション/トリガーのテーブル 'product' を更新できません。*/

次のような 2 つのテーブルと 2 つのトリガーを作成しました。

Trigger 1(テーブル製品の更新後):

BEGIN
 if (old.price <> new.price) then
    insert into product_price
    (product_id, price , date_added)
    values
    (new.product_id, new.price, SYSDATE());
 end if;
END

Trigger 2(テーブルに挿入した後product_price):

BEGIN
 SET @parentid := (select parent_id from product where product_id = new.product_id);
 if (@parentid >0) then
  update  product set price=new.price where product_id in (select @parentid);
 end if;
END

しかし、私は再びそのエラーを受けました。

子の価格が変更されたときに親の価格を更新する必要があるのですが、解決策はありますか?

ありがとう。

4

1 に答える 1