我会尝试这样的事情。
WITH T AS
(SELECT empno, dateintime, @row:=@row+1 rownum FROM
attendance JOIN (SELECT @row:=0) init
ORDER BY empno, timeindate)
SELECT DISTINCT t1.empno
FROM T AS t1 INNER JOIN T AS t2
ON t1.empno = t2.empno AND t1.rownum = t2.rownum + 1
WHERE TIMESTAMPDIFF(DAY, t2.dateintime, t1.dateintime) >= 3
第一个查询按日期时间对表进行排序并枚举行。然后,您将其与自身连接,改变行号,将相邻的日期时间放在一行中。然后,您只需查找具有所需差异的条目。
我不确定它是否有效,但它应该像这样工作:第一个查询:
empno: | timeindate | row
empno 1 | 2013-10-01 00:00:00 | 0
empno 1 | 2013-10-02 00:00:00 | 1
empno 1 | 2013-10-06 00:00:00 | 2
empno 2 | 2013-10-01 00:00:00 | 3
empno 2 | 2013-10-02 00:00:00 | 4
empno 2 | 2013-10-03 00:00:00 | 5
empno 2 | 2013-10-04 00:00:00 | 6
empno 2 | 2013-10-05 00:00:00 | 7
empno 2 | 2013-10-06 00:00:00 | 8
WHERE
没有子句的第二个查询:
empno 1 | 2013-10-02 00:00:00 | 2013-10-02 00:00:00
empno 1 | 2013-10-02 00:00:00 | 2013-10-06 00:00:00
empno 2 | 2013-10-01 00:00:00 | 2013-10-02 00:00:00
empno 2 | 2013-10-02 00:00:00 | 2013-10-03 00:00:00
etc.
如您所见,WHERE
将从该结果中过滤第二行。