0

I want to subtract a number directly from the database. Instead of Retrieving it, Subtracting it then Updating the DB. Is there any way I can do this?

UPDATE Product 
SET stock='$qty' 
where productId = '$productId' and size='$size' and color='$color' 

enter image description here

The stock should be ('numberInDB' - $qty ), is there any way i can do this directly?

Akina
  • 39,301
  • 5
  • 14
  • 25
Dugong98
  • 381
  • 1
  • 5
  • 15
  • *Is there any way I can do this?* Yes, UPDATE allows to use "old" columns values of current row in the expression to be assigned into the column. So `.. SET stock = stock - $qty ..` - i.e. take old `stock` value, substract specified `$qty` value, store result as new `stock` value. – Akina Nov 08 '22 at 09:07

0 Answers0