我要在orders_foods上删除后进行触发
我的逻辑是:delete from orders_foods where id=;id
然后更新订单价格=总和(orders_foods.price),其中id = orders_foods.order_id
我尝试使用此代码,但收到错误#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 ;
答案 0 :(得分:0)
您需要使用old.order_id而不是new.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 ;