Home > Net >  MySQL- Last 6 months including current month
MySQL- Last 6 months including current month

Time:11-22

I have this date format : YYYY-MM-DD (example : 2022-05-10) I want to extract only last 6 months inclunding current months. The result normaly it's : june, july,august, september, octobre, november (6 last months)

I do this request :

Select created_date
from table_A
Where created_date >= now() - INTERVAL 6 MONTH

The query gives me the last 7 months, that is to say from May 2022 to November 2022 and this is not what I want.

I want the last 6 months including the current month, i.e. from June to November

thank you in advance for your help

CodePudding user response:

Simplest way to do this is:

WHERE created_date >= last_day(current_date())   interval 1 day - interval 6 month

CodePudding user response:

Today is 2022-11-21 and you want everything on or after 2022-06-01? That is current date - 5 months - 20 days

SELECT created_date
FROM table_A
WHERE created_date >= (CURRENT_DATE - INTERVAL (DAYOFMONTH(CURRENT_DATE) - 1) DAY) - INTERVAL 5 MONTH;
  • Related