简体   繁体   English

MySQL在现在到去年一个月之间获取记录

[英]MySQL get records between now and a month last year

I am trying to create an SQL query to select records where the timestamp is less than NOW() but is greater than september last year, but i dont want to specify the year by putting something like: 我正在尝试创建一个SQL查询来选择时间戳小于NOW()但大于去年9月的记录,但是我不想通过添加类似以下内容来指定年份:

date > '01/09/2014'

If i were to run the query today it should give me records from: 如果我今天要运行查询,则应提供以下记录:

september 2014 to May 2015 (May as it is the month at the time of creating this question) 2014年9月至2015年5月(5月,因为这是创建此问题的月份)

If i run it next year it should give me records from september 2015 to May 2016 (May as it is the month at the time of creating this question) 如果我明年运行它,它应该给我记录从2015年9月到2016年5月(5月,因为它是创建此问题的月份)

You can construct the date: 您可以构造日期:

where date >= str_to_date(concat(year(now()) - 1, '-09-01'),
                          '%Y-%m-%d') and
      date < now()

I am guessing that you actually want a slightly more complicated condition. 我猜您实际上想要一个稍微复杂一些的条件。 If you run the query at the end of the year (say in December), you probably want September of the current year: 如果在年末(例如十二月)运行查询,则可能需要当年的九月:

where date >= str_to_date(concat(year(now()) - (month(now) < 9), '-09-01'),
                          '%Y-%m-%d') and
      date < now()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM