user3462020
user3462020

Reputation: 61

SQL UPDATE and IF statements

I'm trying to wrap my head around using an IF statement within an UPDATE query.

I've got this code:

UPDATE privatethreads SET to_read = CASE WHEN to_user = 1 THEN '1' ELSE to_read

Which will not execute. I'm not sure what i'm doing wrong as i've been looking at every other thread relevant to this and phpmyadmin only tells me I have an issue after the end of the statement.

Upvotes: 0

Views: 49

Answers (2)

Guneli
Guneli

Reputation: 1731

I think, what you want can be achieved in a more easy way:

UPDATE privatethreads SET to_read ='1' WHERE to_user = 1;

Upvotes: 4

Sadikhasan
Sadikhasan

Reputation: 18600

Try to add END at the end of query

UPDATE privatethreads SET to_read = CASE WHEN to_user = 1 THEN '1' END

Upvotes: 2

Related Questions