2

我的 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 ;
4

2 回答 2

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 ;
于 2012-12-25T14:59:34.267 回答
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 ;
于 2014-11-26T13:31:52.140 回答