Increment a value in Postgres

后端 未结 1 1737
清歌不尽
清歌不尽 2020-12-07 17:12

I\'m a little new to postgres. I want to take a value (which is an integer) in a field in a postgres table and increment it by one. For example if the table \'totals\' had 2

相关标签:
1条回答
  • 2020-12-07 17:43
    UPDATE totals 
       SET total = total + 1
    WHERE name = 'bill';
    

    If you want to make sure the current value is indeed 203 (and not accidently increase it again) you can also add another condition:

    UPDATE totals 
       SET total = total + 1
    WHERE name = 'bill'
      AND total = 203;
    
    0 讨论(0)
提交回复
热议问题