我在myquery中插入逻辑?

时间:2017-06-29 05:14:59

标签: php mysql

Database 'dataimport' tableview:
No  EnNo  INOUT  DateTime
1   12      S    2017-06-02 09:45
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 16:04

Database 'shiftmaster' tableview:
id shifttype starttime endtime
1   Regular   09:30     18:00

数据库tableview'指定详细信息'和字段是

designationname designationid employeeid employeename 
softwareengineer  201          12         xyz

MySqL查询是:

SELECT EnNo,d.employeename, 
    work_dt, 
    SEC_TO_TIME(sum(TIMESTAMPDIFF(SECOND,login,logout))) as time_worked
from (SELECT EnNo, date(DateTime) as work_dt, 
DateTime as login, 
coalesce( 
    (SELECT MIN(DateTime) 
    FROM dataimport as b 
    WHERE a.EnNo = b.EnNo and 
        date(a.DateTime) = date(b.DateTime) and 
        b.DateTime >= a.DateTime and b.INOUT = 'E' ), now()) AS logout
FROM dataimport AS a 
WHERE a.INOUT = 'S' ) as t 
INNER JOIN designation d 
ON t.EnNo = d.employeeid 
GROUP BY EnNo, work_dt 

这在计算总时间时工作正常......

但是如何以这种方式计算迟到时间和早期时间:

1)EnNo  employeename  work_dt     latetime
 12   xyz          2017-06-02     00:15:00
2)EnNo  employeename  work_dt     Earlygoingtime
 12   xyz          2017-06-02     02:00:00

我在查询中做了哪些更改。以上,我已经提到了查询,因此生成结果的方式。

0 个答案:

没有答案