Decrement but don’t go below a value

GREATEST allows you to specify that the greater of multiple values should used. So in this example if the value drops below zero, zero will be greater used:

UPDATE  table
SET     field = GREATEST(0, field - 1)
WHERE   id = $number

Increment but don’t go below a value

LEAST allows you to specify that the lower of multiple values should used. So in this example if the value goes above below 9999, 9999 will be used:

UPDATE  table
SET     field = LEAST(9999, field - 1)
WHERE   id = $number
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.