UPDATE table SET field=field-10; (field is unsigned INT and less than 0).

Discussion in 'PHP' started by x0x, Oct 5, 2011.

  1. #1
    UPDATE table SET field=field-10;
    field is INT and less than 10

    Why does it explode to max int value (4 trillion something)? How are things like this normally handled? Do I have to use multiple queries or php?

    What's the easiest way to make it not explode and go to 0 if the value is negative?
     
    x0x, Oct 5, 2011 IP
  2. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #2
    simplest way to do this is, first you can get field data in one query than do field-10 through php if comes negative set field = 0 using query.


    Sheetal G
     
    SheetalCreation, Oct 5, 2011 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    I was hoping it was a bug of some kind or something like that... Thanks.
     
    x0x, Oct 6, 2011 IP
  4. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #4
    Maybe using 'CASE' or 'IF', so something online these lines:

    UPDATE table SET field = CASE WHEN field < 10 THEN 0 ELSE field-10 END
     
    mfscripts, Oct 7, 2011 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Thanks mfscripts, that's excellent!
     
    x0x, Oct 7, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    An unsigned number is a number without a sigh - IOW there are no negative numbers in the set of unsigned numbers. If you want a negative number saved, you have to change the type of the field to signed integer. (Or do the math yourself: -1 is max integer. -2 is max integer - 1. Etc.)
     
    Rukbat, Oct 7, 2011 IP