質問

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