我遇到MySQL触发器问题。 我有3个表客户,产品和销售。 在销售中,我引用客户和产品,我想更新产品和客户的一些数量在插入新的促销后。 以下触发器无法更新两个表...我无法弄清楚我做错了什么。
DELIMITER $
CREATE TRIGGER OnSalesInsert AFTER INSERT ON Sales
FOR EACH ROW BEGIN
UPDATE Products SET Products.sold=Products.sold+NEW.amount WHERE Products.id=NEW.product_id;
UPDATE Customers SET Customers.amount=Customers.amount+NEW.amount WHERE Customers.id=NEW.customer_id;
END $
DELIMITER ;
答案 0 :(得分:1)
试试这个:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'@'%' */
TRIGGER `OnSalesInsert` BEFORE INSERT ON `Sales`
FOR EACH ROW BEGIN
UPDATE Products
SET sold = sold + new.amount
WHERE id = new.product_id;
UPDATE Customers
SET amount = amount + new.amount
WHERE id = new.customer_id;
END;
$$
DELIMITER ;
答案 1 :(得分:0)
DELIMITER $$
create trigger UpdateAvail after insert on product_details
for each row
Begin
Declare a1 INT;
Declare d1 VARCHAR(1);
Declare d2 VARCHAR(100);
Select count(0) INTO a1 from prod_available where P_Id=new.P_Id;
Select P_Name,P_Brand INTO d1,d2 from product where P_Id=new.P_Id;
IF a1>0 THEN
Update prod_available set P_quantity=P_quantity+new.quantity where P_Id=new.P_Id;
ELSE
insert into prod_available (P_Id,P_Name,P_Brand,P_quantity) values (new.P_Id,d1,d2,new.quantity);
END IF;
END;
$$
DELIMITER ;