开发者

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

开发者 https://www.devze.com 2022-12-28 02:46 出处:网络
I have a table cal开发者_JAVA技巧led 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

I have a table cal开发者_JAVA技巧led 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?


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

UPDATE pollData SET option2 = option2 + 1


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号