Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres incrementing null value in a column

In Postgres 9.3 when a field in a table has a null value the following expression doesn't work:

update table_statatistic set members = members + 1 WHERE user_id = $1; 

However when the field has an integer value then this query increments it by 1 without a problem.

The questions are:

  1. Why is this happening.
  2. How to fix it.
like image 267
Jimski Avatar asked Aug 28 '26 19:08

Jimski


1 Answers

you need to use coalesce for checking null values

update table_statatistic set members = coalesce(members, 0) + 1 WHERE user_id = $1
like image 140
Serkan Arslan Avatar answered Aug 31 '26 13:08

Serkan Arslan