我有一个表名雇员。它有4列:id,name,statuse和date_present。
date_present列包含员工在场和缺席的所有日期。我需要写一个查询来查找是否有员工连续缺席超过3天。
id | name | statuse | date_presnet
1 | khan | present | 2016-12-1
2 | jan | present | 2016-12-2
3 | Kiran| absent | 2016-12-3
4 | jan | absent | 2016-12-4
4 | jan | absent | 2016-12-5
4 | jan | absent | 2016-12-6
答案 0 :(得分:0)
MySQL(或实际上在任何数据库中)的一种方法是使用join
将记录连接到下一个日期:
select t.*
from t join
t t2
on t2.id = t1.id and
t2.date = date_add(t1.date, interval 1 day) and
t2.status = 'absent' and t.status = 'absent' join
t t3
on t3.id = t1.id and
t3.date = date_add(t1.date, interval 2 day) and
t3.status = 'absent' ;