Reputation: 309
My MySQL table looks as follows:
Item_Id Item_Name quantity
1 HPWT 20 20
The quantity field is an integer.
Now, if I bought this "HPWT 20" item again with quantity "5" , I want my table affected as follows:
Item_Id Item_Name quantity
1 HPWT 20 25
What I always do is: first I select the previous quantity from the database, then add new quantity to it, then update the quantity with new one at the desired Item_Id.
Is there another (direct) way to Update the quantity?
Upvotes: 3
Views: 8535
Reputation: 368
Check: Increment value in mysql update query Or: http://www.tech-recipes.com/rx/2139/mysql_increment_an_exisitng_value/
Upvotes: 1
Reputation: 6762
This can simply be achieved like this:
UPDATE
item
SET
quantity = quantity + 5
WHERE
item_id = 1
Upvotes: 0
Reputation: 299
You can execute a UPDATE directly:
UPDATE table SET quantity = quantity + 5 WHERE Item_id = <x>
Upvotes: 4
Reputation: 3206
Yes there is :
UPDATE mytable
SET quantity = quantity + 5
WHERE Item_Id = 1
Upvotes: 0
Reputation: 191819
UPDATE t1 SET quantity = quantity + 5 WHERE item_id = 1;
Upvotes: 0