0

not sure if u face this strange behavior on MySQL table before: when the table field is default as NULL, I do a simple UPDATE statement run no problem. if I were to issue field=field+1 then the value is not update. Then what i did is to insert 0 into the field and run the same field=field+1 again then it works.

UPDATE table1 SET field=field+1 WHERE id=123;

is this expected behavior?

p/s: the field type is double

d4v1dv00
  • 971
  • 4
  • 10
  • 21

2 Answers2

1

If you have a null field then for update use.

//structure
UPDATE TableName SET column = IFNULL(column, 0) + 1 WHERE ...

//original
UPDATE table1 SET field = IFNULL(field, 0) + 1 WHERE id=123;

More details: function_ifnull

Ref# mysql-update-increment-int-field-that-is-null

Community
  • 1
  • 1
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
1

It's because

NULL+1 = NULL

So you have to "trick" it to 0.

UPDATE table1 SET field=COALESCE(field,0)+1 WHERE id=123;
Daniel E.
  • 2,440
  • 1
  • 14
  • 24