简体   繁体   English

MySQL选择上个月完成

[英]MySQL select complete last month

How to select all data from last month (or 30 days)?如何选择上个月(或 30 天)的所有数据?

I already found some answers, and mostly gives this solution我已经找到了一些答案,并且主要给出了这个解决方案

SELECT * 
FROM gigs
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
ORDER BY date DESC

But this gives me also the dates from the future但这也给了我未来的日期

I am only interested in the days from last month or 30 days (not next month and beyond)我只对上个月或 30 天(不是下个月及以后)的天数感兴趣

Is this what you want?这是你想要的吗?

WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND date <= CURRENT_DATE

I added a condition so the query filters on date not greater than today.我添加了一个条件,以便查询过滤日期不大于今天。 I also modified your code so the date range starts one month ago (you had 3 months).我还修改了您的代码,因此日期范围从一个月前开始(您有 3 个月)。

try this code试试这个代码

SELECT * FROM gigs
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
ORDER BY date DESC

You are asking for two separate things.你要求两个不同的东西。

The last 30 days is easy.过去的 30 天很容易。

date between now() - interval 30 day and now()

Data this month is like this:这个月的数据是这样的:

date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now())

Data a few months ago is like this:几个月前的数据是这样的:

date between (last_day(Now() - INTERVAL 4 MONTH) + INTERVAL 1 DAY) 
       and  
             (last_day(Now() - INTERVAL 3 MONTH) + INTERVAL 1 DAY) 

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

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