我有一个查询和一个表。查询"员工培训"包括员工姓名,他们已经采取的培训课程,日期和到期日。我希望LeftJoin返回的是所有可用的培训课程和每个员工缺少的课程,所以在本质上,任何空值。
" ApplicableTraining"总共有5条记录。有些员工只参加了4/5的课程,只有4个记录在" EmployeeTraining"记录。不应该离开加入返回该特定员工的第5个培训项目,其中包含空白" datetaken"和"到期"?
适用培训示例:
示例EmployeeTraining
期望的结果
这是通过Access上的设计视图生成的左连接,但它根本不返回任何空值。
SELECT ApplicableTraining.AppTraining,
EmployeeTraining.Employee,
EmployeeTraining.DateTaken,
EmployeeTraining.Expiry
FROM ApplicableTraining LEFT JOIN EmployeeTraining ON ApplicableTraining.AppTraining = EmployeeTraining.Training;
我在这里上传了我的数据库https://drive.google.com/open?id=0B7foIFlbSH78cFpJOHFsYkxiUlU
答案 0 :(得分:2)
如果该查询没有返回空值,则没有空值
您可以通过添加WHERE EmployeeTraining.Training IS NULL
答案 1 :(得分:1)
你需要在表之间进行CROSS JOIN:Employee和table:LEFT JOIN之后的ApplicableTraining:视图:EmployeeTraining。
CROSS JOIN,将表A中的所有记录与表B连接起来,无论表之间的连接如何。
Sample Applicable Training:
WHMIS
First Aid
CPR
Propane
TDG
Sample Employee
SAM
Result:
SELECT ApplicableTraining.AppTraining, Table[Employee].Employee
FROM ApplicableTraining
CROSS JOIN Table[Employee]
WHMIS|SAM
First Aid|SAM
CPR|SAM
Propane|SAM
TDG|SAM
Desired Results
SELECT Table[Employee].Employee,
ApplicableTraining.AppTraining,
EmployeeTraining.DateTaken,
EmployeeTraining.Expiry
FROM ApplicableTraining
CROSS JOIN Table[Employee]
LEFT JOIN EmployeeTraining ON ApplicableTraining.AppTraining = EmployeeTraining.Training and EmployeeTraining.Employee = Table[Employee]."EmployeeID"
Sam | WHMIS | 05/03/2011 |05/03/2012
Sam | First AID | 06/09/2010 | 06/09/2011
Sam | CPR | 05/03/2011 | 05/03/2012
Sam | Propane | 12/03/2015| 12/03/2018
Sam | TDG | |
答案 2 :(得分:0)
您缺少where条件。请尝试为表格提供别名。
SELECT at.AppTraining,
et.Employee,
et.DateTaken,
et.Expiry
FROM ApplicableTraining at
LEFT JOIN EmployeeTraining et ON at.AppTraining = et.Training
WHERE et.Training IS NULL
答案 3 :(得分:0)
尝试使用不同的存在运算符进行健全性测试,例如
SELECT ApplicableTraining.AppTraining
FROM ApplicableTraining a
WHERE NOT EXISTS ( SELECT *
FROM EmployeeTraining a
WHERE a.AppTraining = e.Training );
如果没有返回任何内容,则没有缺失值。