Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 820
mithun1538 Avatar asked Apr 21 '10 04:04

mithun1538


People also ask

How can I add values to a specific column in MySQL?

First, you must specify the name of the table. After that, in parenthesis, you must specify the column name of the table, and columns must be separated by a comma. The values that you want to insert must be inside the parenthesis, and it must be followed by the VALUES clause.

How do I add a value to an existing column?

INSERT INTO Syntax 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How do I add values to a specific row?

To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.


2 Answers

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

UPDATE pollData SET option2 = option2 + 1
like image 113
Adriaan Stander Avatar answered Sep 19 '22 20:09

Adriaan Stander


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'
like image 29
Karthik Avatar answered Sep 20 '22 20:09

Karthik