我正在使用mysql数据库。我有两个表的错误和配置文件。Bug表有两列(assigned_to,qa_contact),它们通过多对一关系指向配置文件。这些是我查询的简化版本。
首先,我试图这样做,但是它返回重复的行,其中错误表中的qa_contact为null
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)
其次,我的新方法是:
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
但是在第二种方法中,它排除了qa_contact为null的结果。谁能建议一种有效的方法,因为我正在处理数百万个记录,并且想在结果集上添加更多过滤器。
这是LEFT JOIN的作用:
SELECT bug_id, `desc`, dev.assigned_to, qa.qa_contact FROM bugs INNER JOIN profiles as dev ON bugs.assigned_to = dev.userid LEFT OUTER JOIN profiles as qa ON bugs.qa_contact = qa.userid