How to update data from another table using a Trigger

Asked

Viewed 148 times

1

I am trying to register a product in a cart, every time it is registered la significa q was purchased, so whenever this happens I want to decrease the amount in stock, tried with Trigger below, but it did not work:

DELIMITER $$
CREATE TRIGGER Tira BEFORE UPDATE
ON Carrinho
FOR EACH ROW
BEGIN
    IF NEW.Carrinho.Produto = 198 THEN
        UPDATE Produto SET QuantidadeEstoque = (QauntidadeEstoque - 1);
    END IF;
    IF NEW.Carrinho.Produto = 135 THEN
        UPDATE Produto SET QuantidadeEstoque = (QauntidadeEstoque - 1);
    END IF;
    IF NEW.Carrinho.Produto = 244 THEN
        UPDATE Produto SET QuantidadeEstoque = (QauntidadeEstoque - 1);
    END IF;
    IF NEW.Carrinho.Produto = 562 THEN
        UPDATE Produto SET QuantidadeEstoque = (QauntidadeEstoque - 1);
    END IF;

END $$
DELIMITER ;

This is the table:

tabela produto essa com o estoque

  • I noticed an error in the word QauntidadeEstoque... would not be QuantidadeEstoque?

  • corrected and changed nothing

  • that’s right??

  • nor I, have tried everything and not right, I started seeing these triggers today

  • You want it to run as soon as a new cart is registered right?

  • Nathan, can you post the structure of the tables you’re using? It’s easier to see the problem

  • I can, I’ll put.

  • this ai is the product table

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.