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:
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'