title: SQL: Join on two tables where on column is null
body: <p>I am using mysql database. I have two tables bugs and profiles. Bugs table has two columns (assigned_to, qa_contact) pointing to profiles by a many to one relationship. 
  These are the simplified version of my queries.</p>
  
  <p>Firstly, I was trying to do this but it returns duplicate rows where qa_contact is null in the bugs table</p>
  
  <pre><code>select 
    bug_id, 
    desc, 
    dev.assigned_to, 
    qa.qa_contact 
  from 
   bugs, 
   profiles as dev, 
   profiles as qa
  where 
    assigned_to = dev.userid 
    and (qa_contact = qa.userid or qa_contact is null)
  </code></pre>
  
  <p>Secondly, my new approach is:</p>
  
  <pre><code>select bug_id, desc, dev.assigned_to, qa.qa_contact 
  from 
   bugs, 
   profiles as dev, 
   profiles as qa
  where 
    assigned_to = dev.userid 
    and qa_contact = qa.userid
  
   UNION
  
  select bug_id, desc, dev.assigned_to, null 
  from 
   bugs, 
   profiles as dev, 
   profiles as qa
  where 
    assigned_to = dev.userid 
    and qa_contact is null
  </code></pre>
  
  <p>But in the second approach it excludes the result where qa_contact is null. Can anyone suggest a efficient way of doing this because I am dealing with records in order of millions and would like to add more filters on the resultset. </p>