9

I have a table with a column unsigned int, let's name it col1.

I want to decrement the column by a number, if value after decrement is less than 1 then set it to 1. Like this:

UPDATE `tbl1` SET `col1` = GREATEST(1, `col1`-3);

This works fine, but if col1 is 0 or 1 or 2 and I try to do -3 then MySQL throws an error.

I've tried this and this but didn't worked.

2
  • What error?? Perhaps related to UNSIGNED?
    – Rick James
    Jun 24, 2018 at 20:27
  • @RickJames it is, I know, because UNSIGNED doesn't allow negative values.
    – evilReiko
    Jun 25, 2018 at 6:16

4 Answers 4

6

You can use a case clause

update tbl1 set col1 =  (case when col1 <= 3 then 1 else (col1 - 3) end);

As @RDFozz described :

With the OP's listed code, col1 - 3 must be computed, and since it's an unsigned integer, the result must be an unsigned integer. This code prevents the computation from happening unless the result would actually be an unsigned integer.

UPDATE :

Another possible way as suggested by @kondybas

update tbl1 set col1 = IF(col1<=3, 1, col1-3);
4
  • 2
    Excellent point. With the OP's listed code, col1 - 3 must be computed, and since it's an unsigned integer, the result must be an unsigned integer. Your alternate code prevents the computation from happening unless the result would actually be an unsigned integer.
    – RDFozz
    May 7, 2018 at 16:59
  • 2
    The other approach is to use IF() function: SET col1 = IF(col1<3, 1, col1-3)
    – Kondybas
    May 7, 2018 at 21:55
  • DEatTh and @Kondybas answers are both correct and work as expected, though I prefer Kondybas's answer as it seems slightly more readable to me.
    – evilReiko
    May 8, 2018 at 6:05
  • To match the condition ..if value after decrement is less than 1 then set it to 1., then I used <= 3 instead of < 3
    – evilReiko
    May 8, 2018 at 6:24
2

if value after decrement is less than 1 then set it to 1.

Just add a WHERE statement

UPDATE tbl1
  SET col1 = col1 - 1
WHERE col1 >= 2;
3
  • Good answer. But in my case, the - 1 is a variable number, it could be any number (-1, -2, -3, etc), so how to do it?
    – evilReiko
    May 8, 2018 at 5:49
  • 1
    UPDATE tbl1 SET col1 = col1 - $var WHERE col1 >= $var + 1; May 8, 2018 at 5:52
  • That doesn't work as expected: if col1 is 2 and I try to do -3, like this: UPDATE tbl1 SET col1 = col1 - 3 WHERE col1 >= 4; , then col1 will remain as 2 instead of being updated to 1
    – evilReiko
    May 8, 2018 at 6:02
2

Yet another way to skin this cat:

UPDATE tbl1 SET col1 = GREATEST(4, col1) - 3

Possibly more efficient:

UPDATE tbl1 SET col1 = GREATEST(4, col1) - 3
    WHERE col1 > 1;
1

The minimum value that an unsigned int can have is 0, you cannot set it to negative unless it is signed. You need to implement logic to handle the values in the column so your GREATEST(1, 'col1'-3) isn't violating the rule. CASE WHEN in that statement may work as well as pulling out a filtered subset of the data with a subquery.

MySQL Reference

Stack Overflow Reference

You can also cast your unsigned int as a signed int and then perform the arithmetic.

Stack overflow reference on casting to signed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.