title: JOIN different tables depending on condition
body: <p>There is a table of chat participants, <code>data_chats_parties</code>. In this table, there are two fields, <code>team</code> and <code>member</code> which denote if the participant is a member or team account. If team=0 then it is a member account. If team!=0 it is a team account.</p>
  
  <p>If it is a team account, I need to fetch the <code>name</code> column from the <code>data_teams</code> table. Otherwise, I need to fetch the <code>username</code> column from the <code>data_members</code> table.</p>
  
  <p>Obviously this query does not work, but it's what I'm trying to achieve.</p>
  
  <pre><code>SELECT data_chats_parties.*,data_teams.name,data_members.username
  FROM data_chats_parties
    IF ( data_chats_parties.team!=0 THEN
      INNER JOIN data_teams ON data_teams.id=data_chats_parties.team ELSE
      INNER JOIN data_members ON data_members.id=data_chats_parties.member )
  WHERE data_chats_parties.member!=1
  GROUP BY data_chats_parties.id
  </code></pre>
  
  <p>The <code>WHERE data_chats_parties.member!=1</code> line just says "fetch participants who are not me".</p>
  
  <p>Is it possible to achieve this in this manner?</p>