"The same table " LEFT JOIN ON MS-Access 2016 中的“相同的公共字段”

"The same table " LEFT JOINE ON " the same commen field " in MS-Access 2016

我有这样的东西

雇员 (Emp_ID , Emp_Name)
SecurityGuards(主要Emp_ID,替代Emp_ID)。 // 保安人员被视为员工

我的sql查询如下

  Select Employe.Emp_Name 
  From ( Employe
  LEFT JOIN ON SecurityGuards ON SecurityGuards.MainEmp_ID = Employe.Emp_ID)
  LEFT JOIN ON SecurityGuards ON SecurityGuards.AlternativeEmp_ID = Employe.Emp_ID

现在我希望查询按名称而不是 ID 显示主要和备用 Guards,但它只是给我错误提示“不支持连接表达式” 虽然删除第二个 LEFT Join 工作正常。 我正在使用 MS-Access 2016

如果您希望结果在一行中,您可以使用 table 别名和两个 LEFT JOIN

select  . . .
from (Employe as E left join
      SecurityGuards as sgm
      ON sgm.MainEmp_ID = e.Emp_ID
     ) left join
     SecurityGuards as sga
     ON sga.AlternativeEmp_ID = e.Emp_ID;

如果您只想要任一列中的员工,请使用 EXISTS:

select e.*
from employe as e
where exists (select 1
              from SecurityGuards as sg
              where e.Emp_ID in (sg.MainEmp_ID, sg.AlternativeEmp_ID
             );

您必须将 SecurityGuards 加入到 Employee 的 2 个副本才能获得 2 个名称:

SELECT e1.Emp_Name AS MainName, e2.Emp_Name AS AlternativeName
FROM (SecurityGuards AS s 
LEFT JOIN Employee AS e1 ON e1.Emp_ID = s.MainEmp_ID)
LEFT JOIN Employee AS e2 ON e2.Emp_ID = s.AlternativeEmp_ID