查找表1中不在表2中的记录

时间:2018-04-09 22:40:59

标签: sql ms-access

我创建了一个查询,列出已完成课程的员工。我现在正试图列出所有尚未完成某一课程的员工。例如,如果员工没有采取急救措施,我希望列出该名称。

我尝试过使用NOT EXISTS和NOT IN子句,但它们会返回空表或错误。

这是我当前查询列出所有已完成的课程(保存为qryAllCompletedCourses):

SELECT tblEmployee.EmpID, tblEmployee.[LastName] & ", " & [FirstName] AS EmpName, tblCourses.CatelogID, tblEmployeeScheduled.Attended, tblCourses.CourseDateTime
FROM tblCourses INNER JOIN (tblEmployee INNER JOIN tblEmployeeScheduled ON tblEmployee.EmpID = tblEmployeeScheduled.EmpID) ON tblCourses.CourseID = tblEmployeeScheduled.CourseID
WHERE (((tblEmployeeScheduled.Attended)=True))
ORDER BY tblEmployee.[LastName] & ", " & [FirstName]

UNION

SELECT tblEmployee.EmpID, tblEmployee.[LastName] & ", " & [FirstName] AS EmpName, tblIndividualLearning.CatelogID, tblIndividualLearning.Completed, tblIndividualLearning.DateCompleted
FROM tblEmployee INNER JOIN tblIndividualLearning ON (tblEmployee.EmpID = tblIndividualLearning.EmpID) AND (tblEmployee.EmpID = tblIndividualLearning.EmpID)
WHERE (((tblIndividualLearning.Completed)=True));

从那里开始,我希望用户能够选择急救课程(CatelogID = 8)并列出tblEmployee中尚未参加的所有员工。

这就是我所拥有的一切,但它所做的只是返回已完成CatelogID 8的人。

SELECT tblEmployee.EmpID, qryAllCompletedCourses.CatelogID
FROM tblEmployee LEFT JOIN qryAllCompletedCourses ON tblEmployee.EmpID = qryAllCompletedCourses.EmpID
WHERE (((qryAllCompletedCourses.CatelogID)=8));

5 个答案:

答案 0 :(得分:1)

你可以采用不同的方式。您选择的LEFT JOIN方式需要像这样写:

SELECT tblEmployee.EmpID, a.CatelogID
FROM tblEmployee LEFT JOIN 
(SELECT * FROM qryAllCompletedCourses WHERE qryAllCompletedCourses.CatelogID=8) a
ON tblEmployee.EmpID = a.EmpID
WHERE a.EmpID IS NULL;

请注意,上述查询是"特定于访问",因为Access不允许ON子句中的任何条件不是连接表的列之间的直接比较。对于任何其他RDBMS,您将使用此查询:

SELECT tblEmployee.EmpID, qryAllCompletedCourses.CatelogID
FROM tblEmployee LEFT JOIN 
qryAllCompletedCourses 
ON tblEmployee.EmpID = qryAllCompletedCourses.EmpID and qryAllCompletedCourses.CatelogID=8
WHERE a.EmpID IS NULL;

另一种方法是使用NOT IN:

SELECT tblEmployee.EmpID
FROM tblEmployee 
WHERE tblEmployee.EmpID NOT IN (SELECT EmpID FROM qryAllCompletedCourses 
     WHERE qryAllCompletedCourses.CatelogID=8);

要完成这一切,可以通过NOT EXISTS进行操作:

SELECT tblEmployee.EmpID
FROM tblEmployee 
WHERE NOT EXISTS(SELECT 1 FROM qryAllCompletedCourses 
    WHERE qryAllCompletedCourses.CatelogID=8 and 
    tblEmployee.EmpID = qryAllCompletedCourses.EmpID);

答案 1 :(得分:1)

您可以使用NOT IN

SELECT tblEmployee.EmpID
FROM tblEmployee 
WHERE tblEmployee.EmpID NOT IN (SELECT EmpID FROM qryAllCompletedCourses 
     WHERE qryAllCompletedCourses.CatelogID=8);

答案 2 :(得分:0)

您说您已尝试NOT IN ...您的查询是否如此?

SELECT tblEmployee.EmpID, qryAllCompletedCourses.CatelogID
FROM tblEmployee LEFT JOIN qryAllCompletedCourses ON tblEmployee.EmpID = 
qryAllCompletedCourses.EmpID
WHERE tblEmployee.EmpID NOT IN (
                  SELECT tblEmployee.EmpID
                  FROM tblEmployee 
                  LEFT JOIN qryAllCompletedCourses ON tblEmployee.EmpID = qryAllCompletedCourses.EmpID
                  WHERE (((qryAllCompletedCourses.CatelogID)=8))
                                );

答案 3 :(得分:0)

您可以使用" 无与伦比的查询"

例如,要列出Table1ID中不匹配Table2的记录,您可以使用:

SELECT *
FROM Table1 LEFT JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID Is Null;

由于您使用的是Access,最简单的方法是使用查询向导自动构建查询。

  
      
  1. 创建标签之一,在查询组中,点击查询向导
      The Queries group in the Access ribbon displays two options: Query Wizard and Query Design

  2.   
  3. 新查询对话框中,双击查找不匹配的查询向导

  4.   
  5. 选择包含不匹配记录的表格,然后点击下一步   Select a table or query in the Find Unmatched Query Wizard dialog box

  6.   
  7. 选择相关的表格,然后点击下一步。   img

  8.   
  9. 选择与表相关的字段,点击< = >,然后点击下一步。您只能从每个表中选择一个字段。通过查看匹配字段框中的文本,验证是否匹配了正确的字段   Select the matching fields from the tables in the Find Unmatched Query Wizard dialog box
      请注意,如果现有relationships ,则可能已经选择了某些字段。

  10.   
  11. 双击要从第一个表格中查看的字段,然后点击下一步
      Select the fields you want to see in the query output in the Find Unmatched Query Wizard dialog box

  12.   
  13. 您可以选择查看结果或修改查询设计。输入查询的名称,然后单击完成   Enter a name for your unmatched query in the Find Unmatched Query Wizard dialog box

  14.         

    您可能希望修改查询的设计以添加其他条件,更改排序顺序或添加或删除字段。

         

    (改编自Source

更多信息:

答案 4 :(得分:0)

您可以使用SQL左连接:

执行相同的操作
Select * 
FROM T1 
left join  T2 ON T1 .id = T2.id
WHERE T2.id is null