title: MYSQL Inner Join if statement
body: <p>i'm trying to join 3 tables together but use an IF statment to only run a certain query</p>
  
  <p>I have a user table, customer and staff table. The first part below works:</p>
  
  <pre><code>SELECT user.`userID`, `username`,  `user`.email, `registeredBy`, `registeredDate`, 
    CONCAT(staff.firstName, ' ' ,staff.lastName) AS staffName FROM `user` 
     INNER JOIN `level` ON `user`.levelID = `level`.levelID 
      INNER JOIN `staff` ON `user`.registeredBy = `staff`.userID 
  </code></pre>
  
  <p>I need to view who registered the user which shows fine, but then I need to view either the staff name if the user level is 2 or the customer name if the user level is 1</p>
  
  <pre><code>INNER JOIN `staff` ON `user`.userID = staff.userID IF (user.level = 2)
  INNER JOIN `customer` ON user.userID = customer.userID IF(user.level = 1)   
  </code></pre>
  
  <p>Add the above creates the error </p>
  
  <blockquote>
    <p>Not unique table/alias: 'staff'</p>
  </blockquote>
  
  <p>If I remove the inner join for staff I then get the error</p>
  
  <blockquote>
    <p>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(user.level = 2)' at line 6</p>
  </blockquote>
  
  <p>I need to only show the customer name or the staff name depending on the level, but I cannot understand how as I am joing the staff table twice. Once to get the memeber of staff who registered the user and then to view the staff name(or customer). </p>
  
  <p>To help make things easier an example of the page I am displaying is:</p>
  
  <blockquote>
    <p>username | name | account type |
    registered by</p>
  </blockquote>
  
  <p>Hope I haven't made it too confusing to understand.</p>
  
  <p>Thanks.</p>