如何将 1 添加到 mysql 中现有行的列的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2680344/
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-08-31 15:50:24  来源:igfitidea点击:

How to add 1 to the value of a column of an existing row in mysql

mysql

提问by mithun1538

I have a table called pollData. It will always contain only 1 row. It has columns option1, option2, option3, option4, option5 each of type int. In the beginning, these columns have 0 as their value. How do I add 1 to any column, say option2? I mean do i retrieve the value of that column first, perform addition, and store back, or is there any auto increment function?

我有一个名为 pollData 的表。它将始终只包含 1 行。它有列 option1、option2、option3、option4、option5,每个都是 int 类型。一开始,这些列的值为 0。如何将 1 添加到任何列,例如 option2?我的意思是我是先检索该列的值,执行加法并存储回来,还是有任何自动递增功能?

回答by Adriaan Stander

You could try a normal UPDATE, and just replace the column option in question.

您可以尝试普通的UPDATE,只需替换有问题的列选项。

UPDATE pollData SET option2 = option2 + 1

回答by Karthik

Like this you can try :

像这样你可以尝试:

if(isset($option1)) {
       $optadd = " option1 = option1+1";
    } else if(isset($option2)) {
       $optadd = " option2 = option2+1";
    }

UPDATE `tablename` SET ".$optadd." WHERE fieldname = '1'