使用 MySQL 获取当前在线的员工
Get the currently online employees using MySQL
我有员工在不同的班次和工作日工作。他们的班次详细信息以以下格式保存在工作班次 table 中。
- 工作日 - 周一、周二、周三、周四、周五、周六、周日
- StartTime - 24 小时格式。例如,05:00
- EndTime - 24 小时格式。例如,18:00
有些员工上夜班。因此,他们的开始时间为 20:00,结束时间为 05:00(第二天早上)。对于夜班员工,WeekDay 将是 Starttime 工作日。
我现在需要向员工展示 online/working。这是一个实时数据。我目前正在提取当天的记录,然后使用 Java 验证当前时间是否在开始和结束时间之内。如果是,则显示该记录,否则忽略。
由于员工增加,现在整个过程花费的时间太长了。
我想直接使用 SQL 进行此项检查。 StartTime 和 Endtime 字段是 varchar(10) 并且将始终具有 HH:MM
格式
是否可以直接在 SQL 中执行此操作?
如果您使用 date/time 格式存储实际时间,确实会更容易。但是,鉴于数据的格式,您可以使用显式逻辑来确定当前轮班的人员:
select ws.*
from workshifts ws
where (ws.StartTime < ws.EndTime and
date_format(now(), '%h:%i') >= ws.StartTime and
date_format(now(), '%h:%i') <= ws.EndTime and
date_format(now(), '%W') = ws.Weekday
) or
(ws.StartTime > ws.EndTime and
((date_format(now(), '%h:%i') >= ws.StartTime and
date_format(now(), '%W') = ws.Weekday
) or
(date_format(now(), '%h:%i') <= ws.EndTime and
date_format(date_sub(now(), interval 1 day), '%W') = ws.Weekday
)
)
);
我有员工在不同的班次和工作日工作。他们的班次详细信息以以下格式保存在工作班次 table 中。
- 工作日 - 周一、周二、周三、周四、周五、周六、周日
- StartTime - 24 小时格式。例如,05:00
- EndTime - 24 小时格式。例如,18:00
有些员工上夜班。因此,他们的开始时间为 20:00,结束时间为 05:00(第二天早上)。对于夜班员工,WeekDay 将是 Starttime 工作日。
我现在需要向员工展示 online/working。这是一个实时数据。我目前正在提取当天的记录,然后使用 Java 验证当前时间是否在开始和结束时间之内。如果是,则显示该记录,否则忽略。
由于员工增加,现在整个过程花费的时间太长了。
我想直接使用 SQL 进行此项检查。 StartTime 和 Endtime 字段是 varchar(10) 并且将始终具有 HH:MM
格式是否可以直接在 SQL 中执行此操作?
如果您使用 date/time 格式存储实际时间,确实会更容易。但是,鉴于数据的格式,您可以使用显式逻辑来确定当前轮班的人员:
select ws.*
from workshifts ws
where (ws.StartTime < ws.EndTime and
date_format(now(), '%h:%i') >= ws.StartTime and
date_format(now(), '%h:%i') <= ws.EndTime and
date_format(now(), '%W') = ws.Weekday
) or
(ws.StartTime > ws.EndTime and
((date_format(now(), '%h:%i') >= ws.StartTime and
date_format(now(), '%W') = ws.Weekday
) or
(date_format(now(), '%h:%i') <= ws.EndTime and
date_format(date_sub(now(), interval 1 day), '%W') = ws.Weekday
)
)
);