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.
Firstly, I was trying to do this but it returns duplicate rows where qa_contact is null in the bugs table
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)
Secondly, my new approach is:
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
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.