根据“上班打卡”和“下班打卡”查找在特定时间段内未工作的员工

Find employees who haven’t worked between certain times based on ‘clock ins’ and ‘clock outs’

我为具有开始时间和结束时间属性的员工设置了“上班时间”table。 每个员工上下班打卡,table类似这样:

Employee_no Start time            End time
16          2019-04-01 08:00:20   2019-04-02 12:00:10
13          2019-04-01 10:00:20   2019-04-01 14:00:20
31          2019-04-01 14:00:20   2019-04-01 17:00:20

如何根据起止时间找出13点到16点之间没有上班的员工?

您可以使用聚合和 having 子句来显示从未在 13 点到 16 点之间工作的员工:

select employee_no
from mytable
group by employee_no
having max(hour(start_time) <= 16 and hour(end_time) >= 13) = 0

如果您想要 table 中不与 13h-16h 时间段重叠的完整记录列表,则:

select *
from mytable
where not (hour(start_time) <= 16 and hour(end_time) >= 13)

如果你有一个单独的Employee table,最好的方法是比较两个table中的数据。

假设这两个 table 分别称为 Clocks 和 Employees,此查询将查找在 4 月 2 日 13 到 16 之间工作的所有员工:

select Employee_no
from clocks
where Start_time<'2019-04-02 16:00:00' and End_time>'2019-04-02 13:00:00'

将其用作子查询,您可以检查您的员工 table:

select Employee_no
from Employees
where Employee_no not in(
    select Employee_no
    from clocks
    where Start_time<'2019-04-02 16:00:00' or End_time>'2019-04-02 13:00:00'
)