Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query date intervals by month condition

I have a MySql table "departures" with three columns: name, departure_date, return_date.

I need to query those date intervals by specific months as in find out if somebody has moved that month.

This query works if, for example, I want to find all persons movement in June, departure_date or arrival_date contains June (for example: departure date: 05-25-2024 and return_date:06-05-2024:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Select * 
from departures 
where name='".$ame."' 
and (MONTH(departure_date)=6 or MONTH(return_date)=6) 
and YEAR(departure_date) = 2024 and YEAR(return_date) = 2024

But if I have an interval of dates that goes like this: departure_date=05-25-2024 and return_date=07-05-2024 the result is no movement in this month(june).

How can I modify this query so that it will also work with intervals that include the full month of interest?

>Solution :

You can simply do basic comparison for dates in MySql:

SELECT * 
FROM departures 
WHERE name= :name
AND departure_date <= '2024-06-30' AND return_date >= '2024-06-01'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading