暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

mysql中动态取数sql实现

原创 飞天 2025-03-12
76

需求提出

今天项目组同事提出需求,需要根据提供的日期从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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论