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).

Était-ce utile?

La solution

I would try something like:

 UPDATE table SET column = IF(column>number,column-number,0) WHERE customer_id=?;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top