sql—ms access 2016中“同一公共字段”上的“同一表”左joine

2w2cym1i  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(312)

我有这样的东西
雇员(emp\u id,emp\u name)
安全卫士(主emp\u id,备用emp\u 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)来显示主保护和备用保护,但它只会给出一个错误,即“join expression is not supported”,尽管删除第二个左连接可以正常工作。im使用ms access 2016

vs91vp4v

vs91vp4v1#

你必须加入 SecurityGuards 最多2份 Employee 要获取这两个名称:

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
v6ylcynt

v6ylcynt2#

如果要将结果放在一行中,可以使用表别名和两行 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
             );

相关问题