需求提出
今天项目组同事提出需求,需要根据提供的日期从mysql数据库表中动态取数,详细需求如下:根据提供的日期取出当月第一天0点到当天0点的数据,如果当前是1号,则需要取出上个月整月的数据。
举例说明:
如果今天是3月12日,就需要取出3月1日0点 到 3月12日0点的数据;如果今天是3月1日,就需要取出2月1日0点到2月28日23:59:59的数据。
实验过程
构造数据
create table uorder(order_id int,starttime datetime default CURRENT_TIMESTAMP);
INSERT INTO uorder (order_id, starttime) VALUES (1, '2025-03-10 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (2, '2025-03-11 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (3, '2025-03-12 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (4, '2025-02-10 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (5, '2025-02-28 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (6, '2025-01-28 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (7, '2025-01-31 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (8, '2025-03-01 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (9, '2025-02-01 10:00:00');
INSERT INTO uorder (order_id, starttime) VALUES (10, '2024-03-01 10:00:00');
mysql> select * from uorder order by starttime;
+----------+---------------------+
| order_id | starttime |
+----------+---------------------+
| 10 | 2024-03-01 10:00:00 |
| 6 | 2025-01-28 10:00:00 |
| 7 | 2025-01-31 10:00:00 |
| 9 | 2025-02-01 10:00:00 |
| 4 | 2025-02-10 10:00:00 |
| 5 | 2025-02-28 10:00:00 |
| 8 | 2025-03-01 10:00:00 |
| 1 | 2025-03-10 10:00:00 |
| 2 | 2025-03-11 10:00:00 |
| 3 | 2025-03-12 10:00:00 |
+----------+---------------------+
10 rows in set (0.00 sec)
mysql>
复制
编写sql
select * from uorder
where starttime between date_sub(last_day(date_sub('输入日期',interval 1 second)), interval day(last_day(date_sub('输入日期',interval 1 second))) - 1 day) and date_sub('输入日期',interval 1 second)
order by starttime;
复制
测试功能
1、提供的日期是3月12日
预期结果:从表中提取3月1日0点到3月12日0点的数据。执行如下sql:
select * from uorder where starttime between date_sub(last_day(date_sub('2025-03-12',interval 1 second)), interval day(last_day(date_sub('2025-03-12',interval 1 second))) - 1 day) and date_sub('2025-03-12',interval 1 second) order by starttime;
复制
sql实际执行结果如下:
mysql> select * from uorder -> where starttime between date_sub(last_day(date_sub('2025-03-12',interval 1 second)), interval day(last_day(date_sub('2025-03-12',interval 1 second))) - 1 day) and date_sub('2025-03-12',interval 1 second) -> order by starttime; +----------+---------------------+ | order_id | starttime | +----------+---------------------+ | 8 | 2025-03-01 10:00:00 | | 1 | 2025-03-10 10:00:00 | | 2 | 2025-03-11 10:00:00 | +----------+---------------------+ 3 rows in set (0.00 sec)
复制
可以看出,sql实际执行结果和预期结果完全一致。
2、提供的日期是3月1日
预期结果:从表中提取2月1日0点到2月28日23:59:59的数据。执行如下sql:
select * from uorder where starttime between date_sub(last_day(date_sub('2025-03-01',interval 1 second)), interval day(last_day(date_sub('2025-03-01',interval 1 second))) - 1 day) and date_sub('2025-03-01',interval 1 second) order by starttime;
复制
sql实际执行结果如下:
mysql> select * from uorder -> where starttime between date_sub(last_day(date_sub('2025-03-01',interval 1 second)), interval day(last_day(date_sub('2025-03-01',interval 1 second))) - 1 day) and date_sub('2025-03-01',interval 1 second) -> order by starttime; +----------+---------------------+ | order_id | starttime | +----------+---------------------+ | 9 | 2025-02-01 10:00:00 | | 4 | 2025-02-10 10:00:00 | | 5 | 2025-02-28 10:00:00 | +----------+---------------------+ 3 rows in set (0.00 sec)
复制
可以看出,sql实际执行结果和预期结果完全一致。
总结
大家看看有没有更简单的实现方式,欢迎留言交流~~~
关于作者:
网名:飞天,墨天轮2024年度优秀原创作者,拥有 Oracle 10g OCM 认证、PGCE认证以及OBCA、KCP、ACP、磐维等众多国产数据库认证证书,目前从事Oracle、Mysql、PostgresSQL、磐维数据库管理运维工作,喜欢结交更多志同道合的朋友,热衷于研究、分享数据库技术。
微信公众号:飞天online
墨天轮:https://www.modb.pro/u/15197
如有任何疑问,欢迎大家留言,共同探讨~~~
最后修改时间:2025-03-12 17:11:26
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
相关阅读
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
3265次阅读
2025-04-25 18:53:11
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
904次阅读
2025-04-25 15:30:58
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
532次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
456次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
382次阅读
2025-04-15 14:48:05
MySQL 8.0 OCP 1Z0-908 考试解析指南(二)
JiekeXu
349次阅读
2025-04-30 17:37:37
记录MySQL数据库的一些奇怪的迁移需求!
陈举超
302次阅读
2025-04-15 15:27:53
SQL优化 - explain查看SQL执行计划(下)
金同学
296次阅读
2025-05-06 14:40:00
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
数据库运维之道
290次阅读
2025-04-28 11:01:25
MySQL 8.0 OCP 1Z0-908 考试题解析指南
青年数据库学习互助会
287次阅读
2025-04-30 12:17:54