3

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Vikram
  • 309
  • 3
  • 6
  • 19
  • Just so you know, questions are generally written only with a minimum of emboldening/quoting etc - excessive formatting can make text more difficult to read. If you want to render tabular data, paste in the data and use the code button - it saves the need for adding in the ` ` to make stuff line up. – halfer Jun 30 '13 at 19:10

5 Answers5

4

You can execute a UPDATE directly:

UPDATE table SET quantity = quantity + 5 WHERE Item_id = <x>
Hernã Saldanha
  • 300
  • 2
  • 10
1

Check: Increment value in mysql update query Or: http://www.tech-recipes.com/rx/2139/mysql_increment_an_exisitng_value/

Community
  • 1
  • 1
user2520968
  • 358
  • 1
  • 3
  • 11
0
UPDATE t1 SET quantity = quantity + 5 WHERE item_id = 1;
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Yes there is :

UPDATE mytable 
  SET quantity = quantity + 5
  WHERE Item_Id = 1
Dany Caissy
  • 3,176
  • 15
  • 21
0

This can simply be achieved like this:

UPDATE
    item
SET
    quantity = quantity + 5
WHERE
    item_id = 1
Mathieu Rodic
  • 6,637
  • 2
  • 43
  • 49