Vikram
Vikram

Reputation: 309

Update quantity in database

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

Answers (5)

Mathieu Rodic
Mathieu Rodic

Reputation: 6762

This can simply be achieved like this:

UPDATE
    item
SET
    quantity = quantity + 5
WHERE
    item_id = 1

Upvotes: 0

Hernã Saldanha
Hernã Saldanha

Reputation: 299

You can execute a UPDATE directly:

UPDATE table SET quantity = quantity + 5 WHERE Item_id = <x>

Upvotes: 4

Dany Caissy
Dany Caissy

Reputation: 3206

Yes there is :

UPDATE mytable 
  SET quantity = quantity + 5
  WHERE Item_Id = 1

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191819

UPDATE t1 SET quantity = quantity + 5 WHERE item_id = 1;

Upvotes: 0

Related Questions