Pregunta

i got 4 table which call addproduct, sellproduct, reportfoundproduct, productbalance

i want to make a trigger to update the balance quantity of stock in productbalance table after insert data to those three table (addproduct, sellproduct, reportfoundproduct)

below is my trigger in sellproduct table:

CREATE
DEFINER=`root`@`localhost`
TRIGGER `user`.`bad1`
AFTER INSERT ON `user`.`sellproduct`
FOR EACH ROW
BEGIN

UPDATE productbalance 
SET Quantity_OnHand =(Quantity_OnHand- (select (sell_Quantity) from (select * from sellproduct) as temp3)) 
WHERE Product_ID= productbalance.Product_ID ; 

 END$$

its means quantity balance = add product - sales product

but when the trigger exexuted, its change my whole row data - sales product...

how to solve if i just want to change the product ID that changed in value ?

pls help ... thanks

¿Fue útil?

Solución

You need to use the NEW psuedo table so you only use the information in the new row

CREATE DEFINER=`root`@`localhost` TRIGGER `user`.`bad1`
AFTER INSERT ON `user`.`sellproduct` FOR EACH ROW
BEGIN

UPDATE productbalance 
SET Quantity_OnHand = Quantity_OnHand - NEW.sell_Quantity
WHERE Product_ID = NEW.Product_ID ; 

END$$
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top