-->

MySQL Check if date range is in date range

2019-03-03 19:23发布

问题:

Having trouble making a SQL query for a small booking system, i have a table containing two entries which holds two different dates and a number.

Nr    DateFrom      DateTo
----------------------------
1    2015-01-01   2015-01-03
2    2015-01-05   2015-01-08

I want to query where i can enter two different dates and only return the number of the date range in the table that doesn't collide with the entered date range.

For example if i enter the dates 2015-01-02 and 2015-01-04 it would return Nr 2 and 2015-01-05 and 2015-01-08 would return Nr 1.

回答1:

SELECT *
FROM yourTable
WHERE DateFrom > @rangeEnd OR DateTo < @rangeBegin

DEMO