如何只获取工作时间的数据

发布于 2025-01-16 11:50:25 字数 484 浏览 0 评论 0原文

有没有办法只选择或查询工作时间的数据?

id说明datetime
1警报激活2022-01-02 14:00:00
2警报停用2022-01-02 15:00:00
3警报激活2022-01-03 18:00:00
..警报激活2022-01- 31 11:00:00

我想知道电话号码闹钟在周一至周五上午 8 点至下午 5 点的工作时间内启动。

我尝试使用中间日期但没有成功。

Is there a way to select or query the data only on working hours?

iddescriptiondatetime
1Alarm Activated2022-01-02 14:00:00
2Alarm Deactivated2022-01-02 15:00:00
3Alarm Activated2022-01-03 18:00:00
..Alarm Activated2022-01-31 11:00:00

I'd like to get the number of the alarm activated on or during working hours from mon-fri 8am to 5pm.

I tried to use the between date but no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2025-01-23 11:50:25
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
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
注定孤独终老 2025-01-23 11:50:25

这里我们使用以下测试:

  • 工作日 < 6(周六)=周一至周五
  • 7 点以上(08:00:00 起)
  • 17 点以下(16:59:59 起)
创建表警报(  
id 整数,
描述 varchar(100),
日期时间日期时间);
插入警报值
(1,'警报已激活',
'2022-01-02 14:00:00'),
(2,'警报已停用',
'2022-01-02 15:00:00'),
(3,'警报已激活',
'2022-01-03 18:00:00'),
(4,'警报已激活',
'2022-01-31 11:00:00');

<前><代码>选择*
来自警报
其中工作日(日期时间)< 6
且7<小时(日期时间)< 17;

<前>id |描述 |日期时间
-: | :-------------- | :------------------
3 |警报已激活 | 2022-01-03 18:00:00
4 |警报已激活 | 2022-01-31 11:00:00

db<>fiddle 此处

Here we use the following tests:

  • weekday < 6 (Saturday) = Monday to Friday
  • hour more than 7 (from 08:00:00)
  • hour less than 17 (to 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文