我试图想出最好的方法来增加员工的总时间。
INOUT类型是S&即
我认为这样的事情可能是最好的解决方案,但我不知道它是否实用甚至可能:
例如:
No EnNo INOUT DateTime
1 12 S 2017-06-02 08:35
2 28 S 2017-06-02 10:10
3 28 E 2017-06-02 13:00
4 12 E 2017-06-02 14:02
5 12 S 2017-06-02 15:03
6 12 E 2017-06-02 18:09
所以在这个例子中。我可以尝试计算工作时间的总时间。再说一遍,我不知道这是否可能。
我真的很感谢有关如何完成此任务的任何意见/建议,或任何其他可能更实际的方法!
像这样查询:
$query = "SELECT EnNo,
sum(case
when INOUT = 'E' then -DATEDIFF(MINUTE,2017-06-02,DateTime)
when INOUT = 'S' then DATEDIFF(MINUTE,2017-06-02,DateTime)
end
)/60 as Working Hours
From `dataimport`
Group By dataimport.EnNo";
但结果不会产生..
答案 0 :(得分:0)
可行的方法是选择两名员工之间的TIMESTAMPDIFF
。您必须以这样的方式加入表格,使得您在同一天具有S和E的两个员工记录之间存在差异。
我尝试了自己并在未经测试的情况下提出了以下内容,但它应该指向正确的方向:
SELECT
A.EnNo, TIMESTAMPDIFF(MINUTE,A.DateTime,B.DateTime)/60 AS WorkingHours
FROM dataimport A INNER JOIN dataimport B ON A.EnNo = B.EnNo AND
(A.INOUT = 'E' AND B.INOUT = 'S') OR (A.INOUT = 'S' AND B.INOUT = 'E')
AND TIMESTAMPDIFF(DAY,A.DateTime,B.DateTime) = 0
注意:您也可以更改TIMESTAMPDIFF
以一次性查找小时数差异。
参考文献:Calculate the time difference between of two rows https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestampdiff
答案 1 :(得分:0)
另外一种在php中计算的简单方法!!!
$ datetime1 = new DateTime('2017-06-02 08:35');
$ datetime2 = new DateTime('2017-06-02 14:02');
$ duration = $ datetime1-> diff($ datetime2);
echo $ duration->格式('%h')。“小时”。$ duration->格式('%i')。“分钟”;
答案 2 :(得分:0)
Please check the query twice and try with your code ...i have done same scenario which you have
$query->select("a.*,a.DATE_FORMAT(a.date_time,'%Y-%m-%d') as `new_date`,
DATE_FORMAT(a.date_time,'%Y-%m-%d 23:59:00') as `last_datetime`,
DATE_FORMAT(a.date_time,'%Y-%m-%d 00:01:00') as `first_datetime`,
@last_value:=(SELECT date_time FROM master a2
WHERE a2.enroll=a.enroll
and (a2.INOUT = 'E' )
and (a2.`date_time` <= last_datetime and a2.`date_time`>first_datetime)
group by a2.date_time
ORDER BY a2.date_time DESC LIMIT 1) as last_value,
");
$query->from('master as a')
->andWhere('a.enroll IS NOT NULL and a.INOUT="S"')
->groupBy('a.enroll,new_date')
->orderBy('a.enroll,a.date_time');
$attendence_db_data = $query->all();
You will get user's first IN And last OUT of particular one day in one record so that you can easily calculate his/her work hour from that value.