Sql 如何基于第二个表排除记录(使用JOIN)
我认为这很简单,但今天不是这样 我有两个表,第一个表有一列,其中包含第二个表的数据Sql 如何基于第二个表排除记录(使用JOIN),sql,ms-access,Sql,Ms Access,我认为这很简单,但今天不是这样 我有两个表,第一个表有一列,其中包含第二个表的数据 T1 has Column1 value = "Created" T2 has Column1 Value = "Created" and Column2 Value = "OPEN" 因此,我们的想法是返回表1中的每一行,其中表2的第2列=“打开” 这是当前选择 SELECT tblCustIncidents.IncidentID, tblCustIncidents.EntryDateTime, tblCus
T1 has Column1 value = "Created"
T2 has Column1 Value = "Created" and Column2 Value = "OPEN"
因此,我们的想法是返回表1中的每一行,其中表2的第2列=“打开”
这是当前选择
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';
好吧,我发现访问很挑剔。。。所以我试过这个
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');
根据您的查询,您只需将子句添加到
join
中,然后将join
从left
更改为internal
,否则您仍将获得表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'
您能提供示例数据和所需结果吗?如果您使用左联接,您还将从表1中获取列2值未“打开”的行。左连接意味着从左表中选择所有内容,而不管右表中是否找到匹配值。选择tblCustIncidents.IncidentID、tblCustIncidents.EntryDateTime、tblCustIncidents.Title、tblCustIncidents.StatusType、tblCustIncidents.Summary、,tblMaintStatusTypes.StatusDescr从TblCustincints左加入TblCustincints上的tblMaintStatusTypes.StatusType=tblMaintStatusTypes.StatusType,其中tblMaintStatusTypes.OpenOrClosedType='Open';你在做左连接,我已经提到了做常规连接。(内部连接,其中内部单词是可选的)好的,我没想到在access中提到这一点。执行此操作时,我现在收到一个错误“Join expression not supported”…请从中选择tblCustIncidents.IncidentID、tblCustIncidents.EntryDateTime、tblCustIncidents.Title、tblCustIncidents.StatusType、tblCustIncidents.Summary、tblMaintStatusTypes.StatusDescr(tblCustIncidents.StatusType=tblMaintStatusTypes.StatusType和tblMaintStatusTypes.Open或closedType='Open');在这种情况下,只需将其更改为
WHERE
子句,并将JOIN
更改为内部联接
以下是内部联接的完整文档
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'