如何根据第二个表排除记录(使用JOIN)

时间:2014-02-07 14:42:55

标签: sql ms-access

我认为这很简单,但今天却不是这样......

我有两个表第一个有一个包含第二个数据的列。

T1 has Column1 value = "Created"
T2 has Column1 Value = "Created" and Column2 Value = "OPEN"

因此,我们的想法是从表1中返回每一行,其中表2的第2列=“OPEN”

这是当前的SELECT

SELECT tblCustIncidents.IncidentID, tblCustIncidents.EntryDateTime, tblCustIncidents.Title, tblCustIncidents.StatusType, tblCustIncidents.Summary, tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents LEFT JOIN
     tblMaintStatusTypes
     ON tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType;

我以为我可以使用WHERE,但我不清楚WHERE的位置,或者即使它会起作用......我已经看了很多次。我会继续寻找并希望有人能指出我正确的方法/答案/发帖等等。谢谢。

在返回数据的屏幕截图中,最后一列位于两个表中。第二个表格有一个列,表明该项目是打开还是关闭。所以我希望片段中的所有记录都不会出现......我试过这个Select语句......

SELECT tblCustIncidents.IncidentID, tblCustIncidents.EntryDateTime, tblCustIncidents.Title, tblCustIncidents.StatusType, tblCustIncidents.Summary, tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents LEFT JOIN tblMaintStatusTypes ON tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType
WHERE tblMaintStatusTypes.OpenOrClosedType = 'Open';

enter image description here

好的,我发现访问很挑剔......所以我试过了......

    SELECT tblCustIncidents.IncidentID, 
        tblCustIncidents.EntryDateTime, 
        tblCustIncidents.Title, 
        tblCustIncidents.StatusType, 
        tblCustIncidents.Summary, 
        tblMaintStatusTypes.StatusDescr
    FROM tblCustIncidents 
    inner join (SELECT tblCustIncidents.IncidentID, 
        tblCustIncidents.EntryDateTime, 
        tblCustIncidents.Title, 
        tblCustIncidents.StatusType, 
        tblCustIncidents.Summary, 
        tblMaintStatusTypes.StatusDescr
    FROM tblCustIncidents)
    ON (tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType AND tblMaintStatusTypes.OpenOrClosedType = 'Open');

2 个答案:

答案 0 :(得分:0)

    SELECT t1.*
    FROM table1 t1
    JOIN table2 t2
        ON t1.Column1=t2.Column1
    WHERE t2.column2='OPEN'

答案 1 :(得分:0)

根据您的查询,您只需要向join添加子句,然后将joinleft更改为inner,否则您仍然会从表中获取所有行1。

SELECT tblCustIncidents.IncidentID
       ,tblCustIncidents.EntryDateTime
       ,tblCustIncidents.Title
       ,tblCustIncidents.StatusType
       ,tblCustIncidents.Summary
       ,tblMaintStatusTypes.StatusDescr
    FROM tblCustIncidents
    INNER JOIN tblMaintStatusTypes
        ON tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType
    WHERE tblMaintStatusTypes.Column2 = 'OPEN'