如何解决错误 1442:无法更新存储在 mysql 中的 fuction/trigger 中的 table 'tb_name'?

How to solve error 1442 : Can`t update table 'tb_name' in stored fuction/trigger in mysql?

delimiter $$ 
   create trigger payment_mile_trigger
    after insert on PAYMENT
    for each row
    begin
    if @mileuse = 'all' then
        update customer
        set cust_usedmile = cust_usedmile + cust_currentmile
            and cust_currentmile = 0
        where customer.cust_id = new.cust_id;
    elseif @mileuse > 0 then
        update customer
        set cust_usedmile = cust_currentmile + @mileuse
            and cust_currentmile = cust_currentmile - @mileuse
        where customer.cust_id = new.cust_id;
    end if ;
   end $$
delimiter ;
delimiter $$ 
   create trigger Booking_trigger3
    before insert on Booking
    for each row
    begin
      declare pay_totalamount int;
      declare time_stamp timestamp;
      declare pay_type varchar(15);
      declare key_id int;
      declare cust_id int;
      
      set pay_totalamount = new.booking_totalamount;
      set time_stamp = current_timestamp();
      set pay_type = @paytype;
      set key_id = null;
      set cust_id = new.cust_id;
      
    if @mileuse = 'all' then
        insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value ((select cust_currentmile from customer where customer.cust_id=cust_id),
        time_stamp, 'mile',key_id,cust_id);
        insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (pay_totalamount-(select cust_currentmile from customer where cust_id=cust_id),
        time_stamp, pay_type,key_id,cust_id);
    elseif @mileuse > 0 then
        insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (@mileuse,
        time_stamp, 'mile',key_id,cust_id);
        insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (pay_totalamount-@mileuse,
        time_stamp, pay_type,key_id,cust_id);
    else
      insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (pay_totalamount,time_stamp, pay_type,key_id,cust_id);
    end if;
   end $$
delimiter ;

我创建了两个触发器。

一个是触发器,如果​​您在预订时使用里程支付,则在付款中插入两个数据table。 (里程支付,剩余金额通过其他支付方式支付)

还有一个触发器是扣除里程。 如果支付中有使用里程支付的项目table,则从客户那里扣除里程table.

但是我得到一个错误:

Error Code: 1442. Can't update table 'customer' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

我的猜测是这个问题是由于上述两个触发器的冲突。这是因为一个是在使用完所有里程时从客户table中选择数据(里程),而另一个是从客户table.

更新数据(里程)

我发现了两个问题。 首先是里程不扣客户table.

第二个问题是当我设置@mileuse = 'all'时,我得到了与标题相同的错误。

我该如何解决这个问题?

我用稍微不同的方式解决了这个问题。

delimiter $$ 
   create trigger Booking_trigger3
    before insert on Booking
    for each row
    begin
      declare pay_totalamount int;
      declare time_stamp timestamp;
      declare pay_type varchar(15);
      declare key_id int;
      declare cust_id int;
      
      set pay_totalamount = new.booking_totalamount;
      set time_stamp = current_timestamp();
      set pay_type = @paytype;
      set key_id = null;
      set cust_id = new.cust_id;

    if @mileuse > 0 then
        insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (@mileuse,
        time_stamp, 'mile',key_id,cust_id);
        insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (pay_totalamount-@mileuse,
        time_stamp, pay_type,key_id,cust_id);
    else
      insert into payment(pay_totalamount,pay_date,pay_type,key_id,cust_id) 
        value (pay_totalamount,time_stamp, pay_type,key_id,cust_id);
    end if;
   end $$
delimiter ;

我删除了'if @mileuse = all then ...'

select @mileuse := '0';
select @mileuse := 
    (select cust_currentmile from customer
        where cust_id = @custid); # all mile pay 

然后我决定将变量一分为二。

这似乎不是根本解决方案,但它是实现它的最佳方式。