Search code examples
mysqlintminimum

mysql minimum int


I have this query

UPDATE table SET this = this-20 WHERE this >= 0

i dont want this field to ever go under 0. Is there some addition I can add to this query to ensure this, without having to run another query?

eg, if this was 18, then it would be set to -2

(USING MYSQL)


Solution

  • You can make use of the Greatest function:

    UPDATE table SET this = greatest(0,this-20) WHERE this >= 0
    

    that basically set the value of this with the bigger value considering this - 20 and 0.