Reputation: 5568
I have a line of MySQL that I want to use a CASE statement in. However, I don't think I'm doing it right. I need to check if a flag is set to a particular value, and then update that flag based on what it is set to. Here is my MySQL:
$sql = 'UPDATE table SET flag = CASE WHEN flag = 2 THEN 0 ELSE flag = 3'
Is that valid SQL? What I want to do is if the flag is set to a value of 2, set it to 0, else set it to 3. How do I fix my SQL so it does this?
Upvotes: 0
Views: 110
Reputation: 247680
Your current query is very close, the code will be:
UPDATE table
SET flag = CASE WHEN flag = 2 THEN 0 ELSE 3 END
This will update the flag
column to 0
when the flag
is equal to 2
, if it is not equal to 2
, then the value will be updated to 3
.
Upvotes: 1