Ilya Suzdalnitski
Ilya Suzdalnitski

Reputation: 53560

How to update two columns in one statement?

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 = ?

Upvotes: 56

Views: 58091

Answers (3)

Ayman
Ayman

Reputation: 11585

The SQL UPDATE syntax is:

UPDATE table SET
  column1 = value1,
  column2 = value2
WHERE condition

Instead of the AND you need a comma

Upvotes: 55

Jhonny D. Cano -Leftware-
Jhonny D. Cano -Leftware-

Reputation: 18013

Try something like this...

UPDATE exercises
SET times_answered = times_answered + 1,
av_answeringTime = av_answeringTime + ( (av_answeringTime / (times_answered) ) + ?) * (times_answered + 1)
WHERE name = ?

Upvotes: 8

Chad Birch
Chad Birch

Reputation: 74658

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 = ?

Upvotes: 80

Related Questions