Question

I have a table. One column is INT UNSIGNED, so cannot be less than zero. I want to subtract a number but in case the number is greater that the value already, I want to SET it equal to zero.

How can I achieve this?

UPDATE table SET **column = greatest(column-number,0)** WHERE customer_id=? 

doesn't did the trick, but works when number<0 (adds a value).

Was it helpful?

Solution

I would try something like:

 UPDATE table SET column = IF(column>number,column-number,0) WHERE customer_id=?;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top