in my my mysql table, I have a column with date format: yyyy-mm-dd
I make this request to select the maximum and minimum dates for the last 2 months.
SELECT MIN(date) AS date1, MAX(date) AS date2 FROM mytable
WHERE (MONTH(date) = (SELECT Month(MAX(date))-1 FROM mytable )
OR MONTH(date) = (SELECT Month(MAX(date))-2 FROM mytable ))
AND YEAR(date) = (SELECT YEAR(MAX(date))
FROM mytable)
But when I want to select dates for the last 3 months, I found that I can’t make the month Jaunuar-1
and also the request :
OR MONTH(date) = (SELECT Month(MAX(date))-3 FROM mytable )
don’t work !
My question is: How can I select the last 3 Months (or the maximal and minimum dates in the last 3 or 4 or 5 months).
Thanks a lot.
1 Answer