Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    update int field that is null

    Code:
    Update table set field= field+1 where id = '74'
    Field is type INTEGER
    if initial field = null --> no error , no update.

    Any comment on that

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Take a look at the MySQL page entitled "problems with NULL values".

    Basically, any expression with a NULL value ends with a NULL value. In your case, you state that the field is NULL when you start the UPDATE. Adding any value to a NULL is NULL. Thus, the field is technically updating, it's just staying NULL. Make sense?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    There is, however, an easy fix:
    Code:
    UPDATE table SET field = IFNULL(field,0) + 1 WHERE id = '74';


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •