andrew
andrew

Reputation: 5176

Change number in column

When using MySQL, how do you increase or decrease the number in a particular cell by a specified amount with a single query. For example I have a product table with 5 x product a. I sell 1 item and I want to update the field. I want to do it with one query, not get the number add to it and then update (I know how to do that)

Upvotes: 1

Views: 575

Answers (3)

Steve De Caux
Steve De Caux

Reputation: 1779

update products set amount = amount + 1 where productId = 12345

If you are using prepared statements you can replace the amount to add as well as the product id with placeholders

Upvotes: 2

David M
David M

Reputation: 72870

UPDATE Product SET Quantity = Quantity - 1 WHERE Id = xxx

Upvotes: 1

Dor
Dor

Reputation: 7494

UPDATE tbl SET col=col+1 WHERE id=1234

Upvotes: 1

Related Questions