Home > front end >  I would like to get a data between 25th of previous month and 24th of this month using MySQL
I would like to get a data between 25th of previous month and 24th of this month using MySQL

Time:12-08

select Orders, DeliveryDate 
from Shipping 
where DeliveryDate between '2021-11-25' and '2021-12-24';

Now I am hardcoding as above which is not ideal because I need to change the date every month. I am new to SQL. Please help me with this. Thank you.

CodePudding user response:

SELECT Orders, DeliveryDate 
FROM Shipping 
WHERE DeliveryDate BETWEEN DATE_FORMAT(CURRENT_DATE, '%Y-%m-25') - INTERVAL 1 MONTH 
                       AND DATE_FORMAT(CURRENT_DATE, '%Y-%m-24');

DATE_FORMAT(CURRENT_DATE, '%Y-%m-24') forms 24th of current year/month.

DATE_FORMAT(CURRENT_DATE, '%Y-%m-25') - INTERVAL 1 MONTH forms 25th of current year/month and then substracts a month - i.e. it forms 25th of previous month finally.

Both expressions are constants during definite query execution, hence they're calculated during the execution plan building and does not effect the execution time.

  • Related