title: MySQL 5.5 Query Based on IF statement
body: <p>I've been battling with this for too long so I'm here to ask for help...</p>
  
  <p>I have a MySQL stored procedure that I want to do the following:</p>
  
  <p>given an 'id' and a 'username' for a given record</p>
  
  <ol>
  <li>if id does not exist in table then create record </li>
  <li>else if id exists and username is not the same as what exists then update record</li>
  <li>else do nothing</li>
  </ol>
  
  <p>I've tried the following:</p>
  
  <pre><code>BEGIN
  
  DECLARE doCreate INT;
  DECLARE doUpdate INT;
  SELECT COUNT(*) INTO doCreate FROM app_user WHERE id=1;
  IF (doCreate &gt; 0) THEN 
     SELECT COUNT(*) INTO doUpdate FROM app_user WHERE id=1 AND username='other';
  END IF
  
  IF(doCreate = 0) THEN ---SYNTAX ERROR ON THIS LINE---
     SELECT 'CREATE';
  ELSE IF(doUpdate = 0) THEN
     SELECT 'UPDATE';
  ELSE
     SELECT 'NOTHING';
  END IF
  
  END
  </code></pre>
  
  <p>I've also tried replacing the if-elseif-else block with a case statement but get the same result...</p>
  
  <pre><code>CASE ---ERROR ON THIS LINE---
     WHEN doCreate = 0 THEN 
        SELECT 'CREATE';
     WHEN doUpdate = 0 THEN
        SELECT 'UPDATE';
     ELSE
        SELECT 'NOTHING';
  END
  </code></pre>
  
  <ul>
  <li>I seem to get a syntax error on anything that comes after the first END IF, so that's the first problem that occurs...</li>
  </ul>
  
  <p>Any help would be appreciated - I'm sure there's a better way to do this.</p>