MySQL Check if date range is in date range

2019-03-03 19:49发布

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条回答
迷人小祖宗
2楼-- · 2019-03-03 20:18
SELECT *
FROM yourTable
WHERE DateFrom > @rangeEnd OR DateTo < @rangeBegin

DEMO

查看更多
登录 后发表回答