mysqldaterequestmonthcalendar

MySQL- Last 6 months including current month


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


Solution

  • Today is 2022-11-21 and you want everything on or after 2022-06-01?

    That is beginning of current month (2022-11-01) - 5 months:

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