1

I have a PHP application which needs to update multiple product rows. This update should add a given number to the current value of the column called estoque.

Example: add 3 units to product A, 6 units to product B, etc.

Is there way to do this using only one query, like this?

insert into venda_produto (`cod`,`estoque`) values (49477,`estoque`+3), (98798,`estoque`+6) on duplicate key update `cod`=COALESCE( VALUES(`cod`), `cod`),`estoque`=COALESCE( VALUES(`estoque`), `estoque`) 

`

2
  • So your key is cod and you want to increment estoque? Commented Jan 12, 2017 at 10:09
  • Do you know if the rows exist yet? Commented Jan 12, 2017 at 10:10

1 Answer 1

1

This should do what you want:

INSERT INTO vendo_produto (cod, estoque) VALUES (49477, 3), (98798, 6) ON DUPLICATE KEY UPDATE estoque = estoque + VALUES(estoque); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.