【问题标题】:Join two table and get all the employee name with attendance加入两个表并获取所有出勤员工姓名
【发布时间】:2018-02-13 18:38:46
【问题描述】:

我有 2 张桌子 一是员工,二是考勤

员工表:-

考勤表:-

在 AttDate = '2017-09-05' 的考勤表中只有 3 名员工的考勤。

我使用这个查询来加入这些表:-

select EmpName,Attendance from Employee Left join Attendance on Employee.EmpId = Attendance.refId where AttDate ='2017-09-05' 

它显示了这个输出:-

但我需要这个输出(出勤表中未输入的员工也将显示为空出勤):-

我应该在查询中更改什么以获得此输出?

【问题讨论】:

标签: sql join


【解决方案1】:

如果你也加入日期值,结果集不会被淘汰:

select EmpName,Attendance 
from Employee Left join Attendance on Employee.EmpId = Attendance.refId and AttDate ='2017-09-05' 

您还将获得当天没有参加的员工。 查看 this answer 了解更多信息,了解在 where 子句上设置条件与在 on 子句上设置条件之间的区别。

【讨论】:

    【解决方案2】:

    你用WHERE 限制它,所以那天没有参加课程的两个 EmpID 不会出现。

    您可以将where 更改为:

    WHERE AttDate = '2017-09-05' or AttDate is null;

    但这会带回所有日期为空的结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 2019-02-13
      • 2017-01-07
      • 1970-01-01
      相关资源
      最近更新 更多