简体   繁体   English

如何从一个表中获取所有记录,仅从连接表中获取具有条件的记录

[英]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.

相关问题 即使JOINed表中没有对应的记录,如何从一个表中获取所有记录? - How can I get all records from one table, even if there are no corresponding records in the JOINed table? 当其中一个联接表中缺少所有记录时,如何返回所有记录的结果? - How do I return results for all records in a joined table query when they are missing from one of the joined tables? 我需要从表中获取所有记录,其中至少有一个条件 - I need get all records from table joined where at least one case with condition 如何组合连接表中的多个记录 - How to combine multiple records from a joined table 从一个表中删除记录到另一个表 SQL - Deleting records from one table joined onto another table SQL 如何在 SQL 服务器中从第一个和第二个表中获取匹配记录,并且仅从第一个表中获取不匹配记录,该服务器已加入 1 个字段 - How to get a a matching records from 1st and 2nd table and only non matching records from 1st table in SQL Server having joined by 1 field 如何从一张表中选择关系表中不存在的所有记录? - how to select all records from one table that not exist in relation table? SQL:从一个表获取所有记录和从第二个表中获取记录数? - SQL: Get all records from one table AND a count of records from a second table? 如何检索连接表 SQL 的所有记录 - How to retrieve all records of joined table SQL 如果查询 1 条件 1 的部分结果(访问),则 SQL 根据条件 2 获取表 1 中的所有记录 - SQL Get all records from table 1 based on criteria 2 if part of result of Query 1 Criteria 1 (Access)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM