如何计算每个员工每个项目的工作时间

时间:2011-09-27 03:05:08

标签: php mysql

我有一个PHP表单,它将empID,projectNumber和clock-in / clock-out时间戳输入到MySQL表中,如下所示:

没有声望,我无法发布图片,所以看看这里: screenshot http://mailed.in/timecard/ss1.jpg

我需要帮助生成如下所示的报告: screenshot http://mailed.in/timecard/ss2.jpg

我可以在MySQL完全执行此操作吗?怎么样?

2 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助:

SELECT 
    empID AS EmpID, 
    projectNumber AS ProjectNumber, 
    DATE(clocktime) AS StartDate,  
    TIMEDIFF(
      (SELECT max(clocktime) from tableName where DATE( `clocktime` ) = CURDATE( )), 
      (SELECT min(clocktime) from tableName where DATE( `clocktime` ) = CURDATE( ))
    ) AS WorkHours
FROM `tableName`
WHERE 
DATE( `clocktime` ) = CURDATE( )
GROUP BY empID

答案 1 :(得分:0)

尝试此查询 -

CREATE TABLE table_proj (
  empid INT(11) DEFAULT NULL,
  projectnumber INT(11) DEFAULT NULL,
  clocktime DATETIME DEFAULT NULL
);

INSERT INTO table_proj VALUES 
  (1, 1, '2011-09-27 10:02:22'),
  (1, 1, '2011-09-27 11:17:32'),
  (2, 2, '2011-09-27 11:34:13'),
  (3, 3, '2011-09-27 11:01:21'),
  (3, 3, '2011-09-27 13:36:28'),
  (2, 2, '2011-09-27 13:55:39'),
  (4, 4, '2011-09-27 14:25:07');

SELECT
  empid, projectnumber, MIN(clocktime) startdate, TIMEDIFF(MAX(clocktime), MIN(clocktime)) workhours
FROM
  table_proj
GROUP BY
  empid, projectnumber
HAVING
  COUNT(*) = 2;

+-------+---------------+---------------------+-----------+
| empid | projectnumber | startdate           | workhours |
+-------+---------------+---------------------+-----------+
|     1 |             1 | 2011-09-27 10:02:22 | 01:15:10  |
|     2 |             2 | 2011-09-27 11:34:13 | 02:21:26  |
|     3 |             3 | 2011-09-27 11:01:21 | 02:35:07  |
+-------+---------------+---------------------+-----------+