title: Left Join - Unknown Column Error
body: <p>I have been recently going through my database code trying to improve on my old code with new techniques to make it more efficient. Recently I have been looking to JOINs and I spotted what I thought was a perfect opportunity to test it out.</p>
  
  <p>I am currently using the following two queries to form a list of groups for a user:</p>
  
  <pre><code>SELECT g.group_id, g. name 
  FROM assigned_groups ag, user_groups g 
  WHERE ag.user_id=:user_id AND ag.group_id=g.group_id
  
  SELECT g.group_id, g. name 
  FROM users u, user_groups g 
  WHERE u.user_id=:user_id AND u.base_group=g.group_id
  </code></pre>
  
  <p>Those two select statements give me the list but I would like to join them into one. The only catch is a user may or may not have any groups listed in "assigned_groups". Ie thats optional.</p>
  
  <p>From what I understand that means I need a LEFT or RIGHT join. I have currently the following syntax:</p>
  
  <pre><code>SELECT g.group_id, g. name 
  FROM 
    user_groups g, users u 
    LEFT JOIN assigned_groups ag ON ag.user_id=:user_id AND ag.group_id=g.group_id 
  WHERE 
   u.base_group=g.group_id AND u.user_id=:user_id
  </code></pre>
  
  <p><strong>However this is giving me the following error:</strong>
  <em>Unknown column 'g.group_id' in 'on clause'</em></p>
  
  <p>Here is an image of my table structure:
  <img src="https://i.stack.imgur.com/FXC7f.jpg" alt="enter image description here"></p>