title: How do I use an IF statement in an MySQL join query?
body: <p>I have a SQL query that left joins a table in different ways depending on a condition.</p>
  
  <pre><code>SELECT m.id, u.first_name AS otherUser
  FROM matches AS m
  IF (u.id=m.user2ID)
      LEFT JOIN users AS u ON u.id = m.user1ID
  ELSE
      LEFT JOIN users AS u ON u.id = m.user2ID
  ENDIF
  WHERE m.user1ID=2 OR m.user2ID=2
  </code></pre>
  
  <p><code>matches</code> is a table with integer columns user1ID and user2ID. <code>users</code> is a table containing users of my web application. <code>users</code> has a VARCHAR field called <code>first_name</code>.</p>
  
  <p>The intention of this query is to get the names of the users matched with the current user.</p>
  
  <p>However, MySQL returns this error:</p>
  
  <pre><code>#1064 - You have an error in your SQL syntax; 
  check the manual that corresponds to your MySQL server version for 
  the right syntax to use near 'IF (u.id=m.user2ID) LEFT JOIN users 
  AS u ON u.id = m.user1ID ELSE LEFT JOIN user' at line 3
  </code></pre>
  
  <p>Why?</p>