문제

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

도움이 되었습니까?

해결책

I would try something like:

 UPDATE table SET column = IF(column>number,column-number,0) WHERE customer_id=?;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top