title: MySQL CASE to SET an extra field value in one query
body: <p>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.</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>Here is before employee 2 deletes messages:</p>
  
  <pre><code>company_id | to | from | to_del | from_del | viewed
      1        4     2       0         0         0
      1        2     4       0         0         0
  </code></pre>
  
  <p>Here is after employee 2 deletes messages:</p>
  
  <pre><code>company_id | to | from | to_del | from_del | viewed
      1        4     2       0         1         0    
      1        2     4       1         0         0    &lt;-- viewed should be 1
  </code></pre>
  
  <p>Trying this didn't work, but it should be something similar in the same query:</p>
  
  <pre><code>UPDATE messages
  SET to_del=CASE WHEN to=2 THEN 1 ELSE to_del, viewed=1 END, &lt;-- viewed=1 not working
      from_del=CASE WHEN from=2 THEN 1 ELSE from_del END,
  WHERE company_id = '1'
  </code></pre>
  
  <p>How can I make viewed=1 only when the 1st CASE statement is true?</p>