如何只获取工作时间的数据
How to get the data only on working hours
有没有办法select或者只查询工作时间的数据?
id
description
datetime
1
Alarm Activated
2022-01-02 14:00:00
2
Alarm Deactivated
2022-01-02 15:00:00
3
Alarm Activated
2022-01-03 18:00:00
..
Alarm Activated
2022-01-31 11:00:00
我想知道在周一至周五上午 8 点到下午 5 点的工作时间或期间激活的闹钟数量。
我尝试使用中间日期,但没有成功。
SELECT * -- if you need to count them only - use SELECT COUNT(*)
FROM datatable
WHERE WEEKDAY(`datetime`) < 5 -- test weekday
AND TIME(`datetime`) BETWEEN '08:00:00' AND '17:00:00'; -- test hours
这里我们使用以下测试:
- 工作日 < 6(星期六)= 星期一到星期五
- 超过 7 小时(来自 08:00:00)
- 不到 17 小时(到 16:59:59)
create table alarms(
id int,
description varchar(100),
date_time datetime);
insert into alarms values
(1,'Alarm Activated',
'2022-01-02 14:00:00'),
(2,'Alarm Deactivated',
'2022-01-02 15:00:00'),
(3,'Alarm Activated',
'2022-01-03 18:00:00'),
(4,'Alarm Activated',
'2022-01-31 11:00:00');
select *
from alarms
where weekday(date_time) < 6
and 7 < hour(date_time) < 17;
id | description | date_time
-: | :-------------- | :------------------
3 | Alarm Activated | 2022-01-03 18:00:00
4 | Alarm Activated | 2022-01-31 11:00:00
db<>fiddle here
有没有办法select或者只查询工作时间的数据?
id | description | datetime |
---|---|---|
1 | Alarm Activated | 2022-01-02 14:00:00 |
2 | Alarm Deactivated | 2022-01-02 15:00:00 |
3 | Alarm Activated | 2022-01-03 18:00:00 |
.. | Alarm Activated | 2022-01-31 11:00:00 |
我想知道在周一至周五上午 8 点到下午 5 点的工作时间或期间激活的闹钟数量。
我尝试使用中间日期,但没有成功。
SELECT * -- if you need to count them only - use SELECT COUNT(*)
FROM datatable
WHERE WEEKDAY(`datetime`) < 5 -- test weekday
AND TIME(`datetime`) BETWEEN '08:00:00' AND '17:00:00'; -- test hours
这里我们使用以下测试:
- 工作日 < 6(星期六)= 星期一到星期五
- 超过 7 小时(来自 08:00:00)
- 不到 17 小时(到 16:59:59)
create table alarms( id int, description varchar(100), date_time datetime); insert into alarms values (1,'Alarm Activated', '2022-01-02 14:00:00'), (2,'Alarm Deactivated', '2022-01-02 15:00:00'), (3,'Alarm Activated', '2022-01-03 18:00:00'), (4,'Alarm Activated', '2022-01-31 11:00:00');
select * from alarms where weekday(date_time) < 6 and 7 < hour(date_time) < 17;
id | description | date_time -: | :-------------- | :------------------ 3 | Alarm Activated | 2022-01-03 18:00:00 4 | Alarm Activated | 2022-01-31 11:00:00
db<>fiddle here