D
D
Dmitry2017-07-04 22:01:45
MySQL
Dmitry, 2017-07-04 22:01:45

How to formulate a query to the database to select by date range?

There is a table `apartment_dates` that looks like this:

| id | date_from  | date_to    |
|----|------------|------------|
| 1  | 2017-07-01 | 2017-07-10 |
| 2  | 2017-07-11 | 2017-07-20 | 
| 3  | 2017-07-21 | 2017-07-30 |

It is necessary to select records that satisfy a condition that intersects several records:
from 2017-07-05 -- to 2017-07-15 .
I broke my head, not a single idea how to describe such a request.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gill-sama, 2017-07-04
@another_dream

What does cross mean? full entry or partial?
select * from apartment_dates where date_from beetwen '2017-07-05'::date and ' 2017-07-15'::date or date_to '2017-07-05'::date and ' 2017-07-15':: date -- partial
select * from apartment_dates where date_from beetwen '2017-07-05'::date and ' 2017-07-15'::date and date_to '2017-07-05'::date and ' 2017-07- 15'::date --full
C - inclusive
software - exclusive

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question