I’m trying to use a CASE statement on a messages table. I need to mark a message as viewed when the first CASE statement is true.
UPDATE messages
SET to_del=CASE WHEN to=2 THEN 1 ELSE to_del END,
from_del=CASE WHEN from=2 THEN 1 ELSE from_del END,
WHERE company_id = '1'
Here is before employee 2 deletes messages:
company_id | to | from | to_del | from_del | viewed
1 4 2 0 0 0
1 2 4 0 0 0
Here is after employee 2 deletes messages:
company_id | to | from | to_del | from_del | viewed
1 4 2 0 1 0
1 2 4 1 0 0 <-- viewed should be 1
Trying this didn’t work, but it should be something similar in the same query:
UPDATE messages
SET to_del=CASE WHEN to=2 THEN 1 ELSE to_del, viewed=1 END, <-- viewed=1 not working
from_del=CASE WHEN from=2 THEN 1 ELSE from_del END,
WHERE company_id = '1'
How can I make viewed=1 only when the 1st CASE statement is true?
If the logic is really that simple, is there any reason not to go for the straight forward solution;
or, if you really want to eliminate the common subexpression (a bit overkill for to=2)