sql选择和联接表结果缺少2列

b09cbbtk  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(351)

我的查询如下,我想执行join table。

SELECT ce.EmployeeName, ce.EmpNo, cd.DepartmentName, cd.DepartmentCode
 FROM CurrentEmployee ce
 LEFT OUTER JOIN CompanyDepartment cd ON ce.EmpNo = cd.EmpNo 
 LEFT OUTER JOIN (SELECT EmpNo , MIN(ClockIn) AS ClockIn, MAX(ClockOut) AS ClockOut FROM RecordHistory GROUP BY EmpNo ) rh
 ON ce.EmpNo = rh.EmpNo

在执行上述查询之后,结果遗漏了两列 ClockIn 以及 ClockOut 有语法错误吗?

a9wyjsp7

a9wyjsp71#

您应该执行:

SELECT ce.EmployeeName, ce.EmpNo, cd.DepartmentName, cd.DepartmentCode, rh.ClockIn, rh.ClockOut
 FROM CurrentEmployee ce
 LEFT OUTER JOIN CompanyDepartment cd ON ce.EmpNo = cd.EmpNo 
 LEFT OUTER JOIN (SELECT EmpNo , MIN(ClockIn) AS ClockIn, MAX(ClockOut) AS ClockOut FROM RecordHistory GROUP BY EmpNo ) rh
 ON ce.EmpNo = rh.EmpNo
``` `ClockIn` 以及 `ClockOut` 没有出现在您的选择中,这就是为什么您缺少这2列
7dl7o3gd

7dl7o3gd2#

您需要在顶级select子句以及子查询中包含clockin和clockout。

SELECT ce.EmployeeName, ce.EmpNo, cd.DepartmentName, cd.DepartmentCode, rh.ClockIn, rh.ClockOut

相关问题