[英]How to get all records from one table and only records from joined table with criteria
it seems like I've accomplished this before, but struggling again. 好像我之前已经完成了这件事,但又一次挣扎。 Here is my data:
这是我的数据:
tblHotlist
----------
ID
hotlistStatus
buildNumber
loadType
etc
tblessr
-------
ID
esHeadline
notesStatus
actionItems
bugStatusID
etc
tblBugStatus (not needed in query)
------------
ID
bugStatus
etc
tbl_j_hlbug
-----------
esID
hotlistID
timestamp
I want all the records from tblHotlist and if records exist in tblESSR, I need those where bugStatusID=300. 我想要来自tblHotlist的所有记录,如果记录存在于tblESSR中,我需要那些bugStatusID = 300的记录。 I've tried several different joins and subqueries, but still can't get the results I need.
我尝试了几种不同的连接和子查询,但仍然无法得到我需要的结果。 Once I put the qualifier of bugStatusID=300, I only get the records from tblHotlist where records from tblESSR has a bugStatusID of 300.
一旦我把bugStatusID = 300的限定符,我只从tblHotlist获取记录,其中来自tblESSR的记录的bugStatusID为300。
failed attempt: 尝试失败:
SELECT hl.hotlistID, hl.buildnumber, es.ID, es.notesStatus, es.actionItems
FROM tblhotList hl
LEFT OUTER JOIN tbl_j_hlbug j ON j.hotlistID = hl.id
LEFT OUTER JOIN tblESSR es ON j.esrID = es.id
WHERE hl.hotlistStatusID=100 AND hl.loadType='su' AND es.bugStatusID=300
Any help would be appreciated. 任何帮助,将不胜感激。 I've tried different joins and a couple of subqueries, but I always get the same result.
我尝试了不同的连接和几个子查询,但我总是得到相同的结果。
Thanks! 谢谢!
Because of the join, some of your result set BEFORE the WHERE will have NULL in bugStatusID, so you need to add this into your where if you wish to see those results as well. 由于连接,在WHERE之前的一些结果集在bugStatusID中将为NULL,因此如果您希望看到这些结果,则需要将其添加到您的位置。
SELECT hl.hotlistID, hl.buildnumber, es.ID, es.notesStatus, es.actionItems
FROM tblhotList hl
LEFT OUTER JOIN tbl_j_hlbug j ON j.hotlistID = hl.id
LEFT OUTER JOIN tblESSR es ON j.esrID = es.id
WHERE hl.hotlistStatusID=100 AND hl.loadType='su' AND (es.bugStatusID=300 OR es.bugStatusID IS NULL)
You either have to move the limiting criteria to the joins or also look for null values. 您必须将限制条件移动到连接或者还要查找空值。
WHERE hl.hotlistStatusID=100 AND hl.loadType='su' AND
(es.bugStatusID=300 or es.bugStatusID is null)
When the outer joins occur, you have to consider null values will exist on records that don't have matching data. 当外连接发生时,您必须考虑在没有匹配数据的记录上将存在空值。 As such if you try to limit by these, you will end up excluding the nulls w/o matching data;
因此,如果您尝试通过这些限制,最终将排除没有匹配数据的空值; thereby negating the outer join.
从而否定了外连接。 Sometimes this is what you want... sometimes it isn't.
有时这就是你想要的......有时它不是。 In this case I think you wanted the nulls and 300.
在这种情况下,我认为你想要空值和300。
SELECT hl.hotlistID, hl.buildnumber, es.ID, es.notesStatus, es.actionItems
FROM tblhotList hl
LEFT OUTER JOIN tbl_j_hlbug j ON j.hotlistID = hl.id
LEFT OUTER JOIN tblESSR es
ON j.esrID = es.id
AND es.bugStatusID=300
WHERE hl.hotlistStatusID=100 AND hl.loadType='su'
the Hl where clause doesn't matter as you're getting all records to begin with. 当你获得所有记录时,Hl where子句无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.