title: Performing join operation on 3 tables using knex
body: <p>I have a SQL query which performs <code>innerJoin</code> operation on 2 tables.</p>
  
  <p>Table 1: <code>user</code>
  <br /><br />
  <a href="https://i.stack.imgur.com/N3fE7.png" rel="noreferrer"><img src="https://i.stack.imgur.com/N3fE7.png" alt="enter image description here"></a>
  <br /><br />
  Table 2: <code>local_auth</code>
  <br /><br />
  <a href="https://i.stack.imgur.com/WaMaC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/WaMaC.png" alt="enter image description here"></a></p>
  
  <p>The query returns all the matching rows based on the <code>user_id</code> key</p>
  
  <pre><code>async getUser(id) {
      return camelizeKeys(
          await knex
          .select(
              'u.id',
              'u.first_name',
              'u.last_name',
              'u.username',
              'u.image_url',
              'u.is_admin',
              'u.phone',
              'u.info',
              'la.email'
          )
          .from('user AS u')
          .leftJoin('local_auth AS la', 'la.user_id', 'u.id')
          .where('u.id', '=', id)
          .first()
      );
  }
  </code></pre>
  
  <p>Now, what I have to do is to modify the above query so that it performs the join operation on 3 tables.</p>
  
  <p>Table 3: <code>customer_user</code>
  <br /><br />
  <a href="https://i.stack.imgur.com/dPK55.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dPK55.png" alt="enter image description here"></a></p>
  
  <p>There is another table called <code>customer_user</code>, which has <code>user_id</code> as one of the foreign key. How do I retrieve some fields from <code>customer_user</code> table based on the <code>user_id</code>. I would like to perform something like this (but of course the following won’t work due to the incorrect way of performing multiple joins using knex)</p>
  
  <pre><code>async getUser(id) {
      return camelizeKeys(
          await knex
          .select(
              'u.id',
              'u.first_name',
              'u.last_name',
              'u.username',
              'u.image_url',
              'u.is_admin',
              'u.phone',
              'u.info',
              'la.email',
              'cu.customer_id',
              'cu.department_id'
          )
          .from('user AS u')
          .leftJoin('local_auth AS la', 'la.user_id', 'u.id')
          .leftJoin('customer_user AS cu', 'cu.user_id', 'u.id')
          .where('u.id', '=', id)
          .first()
      );
  }
  </code></pre>
  
  <p>Note: Due to the limitation of adding a <code>table</code> markup on SO, I have attached the screenshot instead.</p>