在Postgres中增加一个值 [英] Increment a value in Postgres

查看:107
本文介绍了在Postgres中增加一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Postgres有点陌生。我想在postgres表的字段中获取一个值(它是整数),并将其递增1。例如,如果表总计有2列名称和总计,而Bill总共有203列,那么我将使用什么SQL语句将Bill的总数移动到204?

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 columns, 'name' and 'total', and Bill had a total of 203, what would be the SQL statement I'd use in order to move Bill's total to 204?

推荐答案

UPDATE totals 
   SET total = total + 1
WHERE name = 'bill';

如果您要确保当前值确实为203(并且不会意外增加),也可以添加其他条件:

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;

这篇关于在Postgres中增加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆