我创建了一个查询,列出已完成课程的员工。我现在正试图列出所有尚未完成某一课程的员工。例如,如果员工没有采取急救措施,我希望列出该名称。
我尝试过使用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));
答案 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)
您可以使用" 无与伦比的查询" 。
例如,要列出Table1
中ID
中不匹配Table2
的记录,您可以使用:
SELECT *
FROM Table1 LEFT JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID Is Null;
由于您使用的是Access,最简单的方法是使用查询向导自动构建查询。
在新查询对话框中,双击查找不匹配的查询向导。
选择与表相关的字段,点击,然后点击下一步。您只能从每个表中选择一个字段。通过查看匹配字段框中的文本,验证是否匹配了正确的字段
请注意,如果现有relationships ,则可能已经选择了某些字段。- 醇>
您可能希望修改查询的设计以添加其他条件,更改排序顺序或添加或删除字段。
(改编自Source)
答案 4 :(得分:0)
您可以使用SQL
左连接:
Select *
FROM T1
left join T2 ON T1 .id = T2.id
WHERE T2.id is null