简体   繁体   English

删除THEN UPDATE mysql后触发

[英]Trigger after delete THEN UPDATE mysql

i want make trigger after delete on orders_foods 我要在orders_foods上删除后进行触发

my logic is: delete from orders_foods where id=;id then update price on orders price = sum (orders_foods.price ) where id=orders_foods.order_id 我的逻辑是: delete from orders_foods where id=;id然后在id = orders_foods.order_id的订单价格=总和(orders_foods.price)上更新价格

this my code i try befor but got error #1363 - 这是我尝试的代码,但出现错误#1363 -

Delimiter //  
CREATE TRIGGER ubah_tot_harga AFTER DELETE ON orders_foods
FOR EACH ROW
BEGIN
    DECLARE harga_1 int default 0;
     SELECT SUM(price) into harga_1 fROM `orders_foods` WHERE order_id = new.order_id;

     update  orders
      set price= harga_1
      where id=old.order_id;
      END
//
delimiter ;

orders table orders_foods table 订单表 orders_foods表

Instead of new.order_id, you need to use old.order_id. 您需要使用old.order_id而不是new.order_id。 because after delete there is no new.order_id there is only old.order_id. 因为删除后没有new.order_id,只有old.order_id。

Delimiter //  
CREATE TRIGGER ubah_tot_harga AFTER DELETE ON orders_foods
FOR EACH ROW
BEGIN
    DECLARE harga_1 int default 0;
     SELECT SUM(price) into harga_1 fROM `orders_foods` WHERE order_id = old.order_id;

     update  orders
      set price= harga_1
      where id=old.order_id;
      END
//
delimiter ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM