我有 3 个表:员工(员工 ID、FN、LN)、必修培训(培训 ID、培训名称)、已完成培训(完成 ID、员工姓名(来自员工的查找字段)、培训名称(来自必修培训的查找字段)、完成日期)
然后我创建了查询来显示表中的所有信息
SELECT CaregiverInformation.CaregiverID, CaregiverInformation.FirstName, CaregiverInformation.LastName
FROM CaregiverInformation;
SELECT CaregiverCompletedTraining.TrainingRecordID, CaregiverCompletedTraining.Caregiver, CaregiverCompletedTraining.Training, CaregiverCompletedTraining.CompletedDate, CaregiverCompletedTraining.ExpiredDate, IIf(Date()>=[ExpiredDate],"Expired","Current") AS IsExpired
FROM CaregiverCompletedTraining;
SELECT RequiredTrainings.TrainingID, RequiredTrainings.TrainingName, RequiredTrainings.Required
FROM RequiredTrainings;
如果员工参加了培训,则会在已完成培训表中进行跟踪。我需要创建一个查询,该查询将显示员工表中所有员工的必修培训中列出的所有培训,无论他们是否在已完成培训表中。但是,如果它在已完成培训表中,我希望它列出接受培训的日期。
我可以使用以下 SQL 代码查询列出所有接受过培训的员工
SELECT qry_CaregiverInformation.CaregiverID, qry_CaregiverInformation.FirstName, qry_CaregiverInformation.LastName, qry_RequiredTrainings.TrainingName
FROM qry_CaregiverInformation, qry_CompletedTrainings, qry_RequiredTrainings
WHERE (((qry_CaregiverInformation.CaregiverID) Like "*") AND ((qry_RequiredTrainings.TrainingName) Like "*"));
但是我无法让完成的信息正常工作。我尝试了 union,但列数不匹配。如果我在 SQL 中添加空列,则会收到数据不匹配错误。任何帮助都将不胜感激。
答案1
这只是一个简单的左连接:
select *
from CaregiverInformation i
left join RequiredTrainings r on r.CaregiverID = i.CaregiverID
left join CaregiverCompletedTraining c on c.TrainingRecordID = r.TrainingRecordID
如果你只想在 r 存在的情况下限制 i,那么请使用内连接:
select *
from CaregiverInformation i
inner join RequiredTrainings r on r.CaregiverID = i.CaregiverID
left join CaregiverCompletedTraining c on c.TrainingRecordID = r.TrainingRecordID
在这里阅读有关连接之间的差异的更多信息:https://stackoverflow.com/questions/448023/left-right-outer-and-inner-joins 之间的区别是什么