我2个表的确切sql查询字符串

时间:2013-04-04 13:58:21

标签: sql ms-access-2007

我的兄弟帮我设计了访问2007数据库的Sql字符串

   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)的信息,员工的ID,姓名,THours(工作总时数)

示例结果:

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

知道如何实现吗?先谢谢你..

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;