I
I
Ivan Ilyasov2018-07-30 07:55:55
MySQL
Ivan Ilyasov, 2018-07-30 07:55:55

How, when updating one value in the table, to update another, so that the updated value is a number with -10 from the first?

I don’t know how to formulate correctly in MySql I don’t understand well.
Given :
The test_table table has columns id_product , quantity and quantity_stock - everywhere Integer
Needed :
So that when the quantity column is updated , the quantity_stock column takes the value = the new value of the quantity column - 10
That's all that was enough for me, and apparently I don't understand anything :

CREATE TRIGGER update_test AFTER UPDATE ON test_table
FOR EACH ROW
BEGIN
IF NEW.quantity <=> OLD.quantity THEN
  UPDATE test_table SET quantity_stock = NEW.quantity - 10;
END IF;
END;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kumanin, 2018-07-30
@IvanIlyasov

Change the trigger to BEFORE UPDATE and just set the new value before the change.

CREATE TRIGGER update_test BEFORE UPDATE ON test_table
FOR EACH ROW
BEGIN
IF NEW.quantity <> OLD.quantity THEN
  SET NEW.quantity_stock = NEW.quantity - 10;
END IF;
END;

R
Rsa97, 2018-07-30
@Rsa97

And what prevents to make such change directly in request?

UPDATE `test_table` 
  SET `quantity` = :newQuantity, `quantity_stock` = :newQuantity-10
  WHERE `id_product` = :productId

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question