title: MySQL - on duplicate key - CASE WHEN THEN ELSE doesnt work
body: <p>I want to insert an entry (uid, A, B) to database. If the entry is existed already, I will update the 'A' and 'B' column with condition:
  (<a href="https://stackoverflow.com/users/3660572/p-jairaj">1</a> = old entry; [2] = new entry)</p>
  
  <pre><code>A = (B[1] == B[2]) ? A[1] : A[2];
  B = B[2] + 1;
  </code></pre>
  
  <p>My query:</p>
  
  <pre><code>INSERT INTO TableName (uid,A,B) 
  VALUES (uid,A[2],B[2]) 
  ON DUPLICATE KEY 
  UPDATE  A= (SELECT CASE B WHEN B[2] THEN A ELSE A[2] END), 
          B= B[2]+1;
  </code></pre>
  
  <p>Ex1: </p>
  
  <pre><code>Old entry : uid = 1; A = 2; B = 4;
  New entry : uid = 1; A = 3; B = 4;
  -&gt; Should be: A = 2; B = 5;
  But my result: A = 3; B = 5; -&gt; FAIL
  </code></pre>
  
  <p>Ex2: </p>
  
  <pre><code>Old entry : uid = 1; A = 2; B = 4;
  New entry : uid = 1; A = 3; B = 6;
  -&gt; A = 3; B = 7; (my code works well in this ex) -&gt; OK
  </code></pre>
  
  <p>Thank you very much.</p>
  
  <p>Answer 1: remove SELECT key make it work</p>
  
  <pre><code>INSERT INTO TableName (uid,A,B) 
  VALUES (uid,A[2],B[2]) 
  ON DUPLICATE KEY 
  UPDATE  A= (CASE B WHEN B[2] THEN A ELSE A[2] END), 
          B= B[2]+1;
  </code></pre>
  
  <p>Answer of <a href="https://stackoverflow.com/users/3660572/p-jairaj">p-jairaj</a></p>
  
  <pre><code>INSERT INTO TableName (uid,A,B) 
  VALUES (uid,A[2],B[2]) 
  ON DUPLICATE KEY 
  UPDATE  A= (IF(B=B[2],A,A[2])), 
          B= B[2]+1;
  </code></pre>