MySQL获取上个月的具体日期

发布于 2025-01-17 10:30:37 字数 800 浏览 0 评论 0原文

由于20th上个月,我想选择所有行。

我知道我可以在一个月前使用选择一个日期选择() - Interval 1个月,但是如何在该月设置特定的一天?

今天是3月28日,我想选择比2月20日更新的所有行。

即使我可以直接设置这一天,如果查询日期为31st,它在2月不起作用,而应该选择该月的最后一天。

注意:第20位只是一个示例,该值是在代码中的其他地方动态生成的。

一些示例:

Day: 20th each month
Now: March 28th | Query: February 20th
Now: May 28th | Query: April 20th
Day: 31st each month
Now: March 28th | Query: February 28th // we select last day instead of 31st
Now: May 28th | Query: April 30th // we select last day instead of 31st

完整查询将是select *从sales中select * where date> set_day(20,现在() - 间隔1个月)

那么,我该用什么替换为set_day(也适用于较短的月份)?

一个非常常见的用例是计费期,您想在当前的计费周期内选择所有数据。

I want to select all rows since 20th last month.

I know I can select a date one month ago using SELECT NOW() - INTERVAL 1 MONTH, but how can I set a specific day in that month?

Today is March 28th, I want to select all rows newer than February 20th.

Even if I could set the day directly, if the queried date was 31st that wouldn't work for February, where instead it should select the last day of that month.

Note: 20th is just an example, the value is dynamically generated somewhere else in the code.

Some examples:

Day: 20th each month
Now: March 28th | Query: February 20th
Now: May 28th | Query: April 20th
Day: 31st each month
Now: March 28th | Query: February 28th // we select last day instead of 31st
Now: May 28th | Query: April 30th // we select last day instead of 31st

The full query would be something like SELECT * FROM sales WHERE date > SET_DAY(20, NOW() - INTERVAL 1 MONTH).

So, what can I replace that SET_DAY with (that also works with shorter months)?

A pretty common use case for this are billing periods, where you want to select all data from within the current billing cycle.

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

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

发布评论

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

评论(3

被翻牌 2025-01-24 10:30:37

好吧,我相信这可以解决问题

select now()
- interval 1 month
- interval(
   day(now())
   - least(
      20,
      day(last_day(now() - interval 1 month))
     )
  ) day

Well, I believe this solves the problem

select now()
- interval 1 month
- interval(
   day(now())
   - least(
      20,
      day(last_day(now() - interval 1 month))
     )
  ) day
迷途知返 2025-01-24 10:30:37

假设您在客户端中有一个参数是目标天数,并且您想在 sql 中验证它(确保您得出上个月内的某一天),而不是在客户端中,我会这样做:

least(
    date(date_format(current_date - interval 1 month, '%Y-%m-01')) + interval ? - 1 day,
    date(date_format(current_date, '%Y-%m-01')) - interval 1 day
)

一个警告: current_date 将是连接时区的当前日期,由客户端控制;如果我知道我想要这一天的时区,我更喜欢始终明确,例如:

date(convert_tz(utc_timestamp(), '+00:00', 'America/Los_Angeles'))

而不是仅 current_date (两个地方)。

Assuming you have a parameter in the client that is the target day number, and you want to validate it in the sql (making sure you come up with a day inside the previous month), not in the client, I would do:

least(
    date(date_format(current_date - interval 1 month, '%Y-%m-01')) + interval ? - 1 day,
    date(date_format(current_date, '%Y-%m-01')) - interval 1 day
)

One caution: current_date will be the current date in the timezone of the connection, which is controlled by the client; I prefer to always be explicit if I know what timezone I want the day in, like:

date(convert_tz(utc_timestamp(), '+00:00', 'America/Los_Angeles'))

instead of just current_date (both places).

安穩 2025-01-24 10:30:37

我只是意识到我的问题并未包括所有情况。如果最后一个计费日期在本月内,则应计算本月的20日(不是先前的)。

根据@ysth的答案,我添加了一个IF选择本月20的20世纪(如果我们通过它),否则上个月:

IF(DAY(current_date) >= 20, 
   date(date_format(current_date, '%Y-%m-01')) + interval 20 - 1 day,
    least(
        date(date_format(current_date - interval 1 month, '%Y-%m-01')) + interval 20 - 1 day,
        date(date_format(current_date, '%Y-%m-01')) - interval 1 day
    )
);

I just realized that my question didn't include all cases. If the last billing date is within this month, it should count the 20th of this month (not previous one).

Based on @ysth answer, I added an IF to select 20th of this month (if we are passed it), otherwise of previous month:

IF(DAY(current_date) >= 20, 
   date(date_format(current_date, '%Y-%m-01')) + interval 20 - 1 day,
    least(
        date(date_format(current_date - interval 1 month, '%Y-%m-01')) + interval 20 - 1 day,
        date(date_format(current_date, '%Y-%m-01')) - interval 1 day
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文