I need to get the overlapping date row from DB where my entered start and end dates overlaps with any of the rows in the DB.
Query:
SELECT *
FROM dates
WHERE date_from >= $dateFrom
AND date_from >= $dateTill
OR date_till >= $dateFrom
AND date_till <= $dateTill
OR date_from <= $dateFrom
AND date_till >= $dateTill
Currently I have a row with start date: 2019-03-11 and end date 2019-03-17
And query skips it at the moment with these dates:
Beginning: 2019-03-07
End: 2019-03-16
Any ideas how can I improve the query?
Try this:
SELECT *
FROM dates
WHERE (date_from BETWEEN $dateFrom AND $dateTill)
OR (date_till BETWEEN $dateFrom AND $dateTill)
OR ($dateFrom BETWEEN date_from AND date_end)
You have to use parentheses when doing some complicated logical operations.
Also look how logical operators work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators