mysqldecrement

Decrement value in mysql but not negative


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


Solution

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

    UPDATE your_table 
    SET field = field - 1
    WHERE id = $number
    AND field > 0