0

我哥帮我设计了一个sql字符串,用于我的access 2007数据库

   SELECT Employee.EmpID, Employee.Name, Sum(DateDiff("s",Punches.LogIN,Punches.LogOUT)) AS THours
FROM Punches INNER JOIN Employee ON Punches.EmpID=Employee.EmpID
GROUP BY Employee.Name, Employee.EmpID;

此 sql 字符串将显示来自两个表 (Employee, Punches) 的信息 Employee's ID, Name, THours (Total Hours of work)

样本结果:

EmpID   Name    THours
0001    Emp1    14560
0003    Emp3       13
0004    Emp4    43585

我的问题是我还想在打孔表中显示那些还没有总工作时间但已经在员工表中的员工。

像这样

EmpID   Name    THours
    0001    Emp1    14560
    0002    Emp2       0
    0003    Emp3       13
    0004    Emp4    43585

知道如何实现吗?先感谢您..

4

2 回答 2

2

你应该使用LEFT JOIN

SELECT Employee.EmpID, Employee.Name, Sum(NZ(DateDiff("s",Punches.LogIN,Punches.LogOUT),0)) AS THours
FROM Employee 
LEFT JOIN Punches  
    ON Punches.EmpID=Employee.EmpID
GROUP BY Employee.Name, Employee.EmpID
ORDER BY Employee.EmpID;
于 2013-04-04T14:05:48.040 回答
0
SELECT Employee.EmpID, Employee.Name, Sum(DateDiff("s",COALESCE(Punches.LogIN, 0),COALESCE(Punches.LogOUT, 0)) AS THours
FROM Employee 
LEFT JOIN Punches
ON Punches.EmpID=Employee.EmpID
GROUP BY Employee.Name, Employee.EmpID;
于 2013-04-04T14:02:21.810 回答