Decrement value in mysql but not negative

user2244804 picture user2244804 · Apr 29, 2013 · Viewed 43.9k times · Source

I want to decrement a value when user delete it in php and mysql. I want to check not to go below than 0. If value is 0 then do not decrement.

mysql_query("UPDATE table SET field = field - 1 WHERE id = $number");

If field is 0 then do not do anything

Answer

juergen d picture juergen d · Apr 29, 2013

Add another condition to update only if the field is greater 0

UPDATE table 
SET field = field - 1
WHERE id = $number
and field > 0