如何获得日期和工作时间总数在MYSQL中参考checkin&退房时间

时间:2015-03-17 04:38:44

标签: php mysql

下表显示了checkin&员工结账时间

Emp_id report_date report_time 

 11     2014-12-01 08:02:31
 21     2014-12-01 08:13:04
 11     2014-12-01 18:03:41
 21     2014-12-01 16:36:02

1 个答案:

答案 0 :(得分:0)

您需要将时间值转换为秒检查此帖子:

MySQL: how to get the difference between two timestamps in seconds

我会与您分享一份有效的代码,但重要的是要了解其中的内容。

SELECT `Emp_id`, `report_date`, min(`report_time`) AS chekin, 

max(`report_time`) AS chekout,

( (TIME_TO_SEC(TIMEDIFF(max(`report_time`), min(`report_time`))) / 60) / 60) difference


FROM `your_table` WHERE 1 GROUP BY `Emp_id`, `report_date`

首先按员工ID和报告日期进行分组,这样您就可以在不同的日期为每个ID获取一行。然后,您选择该员工在该给定日期的最短时间,以及最长时间。然后你得到时间差(以秒为单位),将它除以60,这样你就能在几分钟内得到它,然后再将它除以60,这样你就可以在几小时内得到它。