Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update two columns in one statement?

Tags:

sqlite

How can I update 2 columns at a time?

I tried the following statement, which doesn't work:

UPDATE exercises SET times_answered = times_answered + 1 AND av_answeringTime = av_answeringTime + ( (av_answeringTime / (times_answered) ) + ?) * (times_answered + 1) WHERE name = ? 
like image 566
Ilya Suzdalnitski Avatar asked Apr 30 '09 18:04

Ilya Suzdalnitski


People also ask

How do I UPDATE multiple columns in one UPDATE statement?

We can update single columns as well as multiple columns using UPDATE statement as per our requirement. UPDATE table_name SET column1 = value1, column2 = value2,... WHERE condition; table_name: name of the table column1: name of first , second, third column.... value1: new value for first, second, third column....

How do you UPDATE multiple columns of multiple rows in one SQL statement?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

Can we UPDATE two columns in a single query in MySQL?

MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.


1 Answers

Use a comma instead of your "AND":

UPDATE exercises SET times_answered = times_answered + 1,     av_answeringTime = av_answeringTime + ( (av_answeringTime / (times_answered) ) + ?) * (times_answered + 1) WHERE name = ? 
like image 136
Chad Birch Avatar answered Sep 30 '22 19:09

Chad Birch