SQL - 根据记录选择要写入的字段

时间:2016-11-01 17:44:08

标签: sql ms-access

以下代码按预期工作:

SELECT Sessions.*, Register.EmailAddress AS TeamleadAddress
FROM Register, Employees INNER JOIN Sessions ON Employees.EmpNumber =  Sessions.EmpNumber
WHERE (((Register.Username)=[Employees].[Teamlead]));

我需要在同一个查询中使用另一个列:

Register.EmailAddress AS QualityAddress WHERE ((Register.Username)=[Employees].[Quality]));

可悲的是,我自己并不能自己解决这个问题......提前致谢!

1 个答案:

答案 0 :(得分:2)

您需要额外JOINRegister。在MS Access中,这也需要一堆括号:

SELECT s.*, rtl.EmailAddress AS TeamleadAddress, rq.EmailAddress
FROM ((Sessions as s LEFT JOIN
       Employees as e
       ON e.EmpNumber = s.EmpNumber
      ) LEFT JOIN
      Register as rtl
      ON rtl.UserName = e.Teamlead
     ) LEFT JOIN
     Register as rq
     ON rq.UserName = e.Quality;

这使用LEFT JOIN,因此,如果NULL中的某列为Employees,则查询仍将返回该行。