Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1175

Warning: session_start(): Cannot start session when headers already sent in /home/styllloz/public_html/qa-include/app/users.php on line 162

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/users.php on line 1267

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/page.php on line 356
As when you update one of the values in the table to update the other, so that the updated value was a number from -10 from the first? - code-flow.club | Q&A

As when you update one of the values in the table to update the other, so that the updated value was a number from -10 from the first?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
38 views
Don't know how to formulate correctly in MySql, not good.

This:
Table test_table in her columns id_product, quantity and quantity_stock - everywhere Integer

Need:
So when you update the column quantitycolumn quantity_stock took a value = new value of quantity - 10

Here at all that I had, and apparently, I don't understand:
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;
by | 38 views

2 Answers

0 like 0 dislike
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;
by
0 like 0 dislike
And what hinders to make such changes directly in the query?
UPDATE `test_table` SET `quantity` = :newQuantity, `quantity_stock` = :newQuantity-10 WHERE `id_product` = :productId
by
110,608 questions
257,186 answers
0 comments
35,181 users