postgresql 在 Postgres 中增加一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10233298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:27:25  来源:igfitidea点击:

Increment a value in Postgres

postgresqlincrement

提问by greatwitenorth

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?

我对 postgres 有点陌生。我想在 postgres 表的一个字段中取一个值(它是一个整数)并将其递增一。例如,如果表 'totals' 有 2 列,'name' 和 'total',而 Bill 的总数为 203,那么我将使用什么 SQL 语句将 Bill 的总数移至 204?

回答by a_horse_with_no_name

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:

如果您想确保当前值确实是 203(并且不会意外地再次增加),您还可以添加另一个条件:

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