Skip to content
Advertisement

MySQL select complete last month

How to select all data from last month (or 30 days)?

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)

Advertisement

Answer

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).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement