How to compile a sample of MySQL dates?

Hi. You can choose the records that are included in the period
In the database the table contains records with fields date_from and date_to
For example, in the database there are records
id | date_from | date_to
--------------------------------------------
1 | 2019-03-15 | 2019-03-30
2 | 2019-02-12 | NULL
3 | 2019-04-10 | 2019-05-25
4 | 2019-01-01 | 2019-02-02

NULL - indicates that no action date is limited, that is valid at the current time, too
You need to select all records that are between 2019-03-12 for 2019-03-26
Turns out when our condition should be selected 1 and 2 record as they enter into these periods.
So, how to make a sample?
March 19th 20 at 08:38
2 answers
March 19th 20 at 08:40
Solution
The condition of intersection intervals:
begin1 <= end2 && begin2 <= end1
Which in my case applies to begin1, begin2, etc? - germaine_Lang commented on March 19th 20 at 08:43
@germaine_Lang, the Beginning and the end of the first and second intervals - Reilly46 commented on March 19th 20 at 08:46
@Reilly46at I understand. The first and second interval? The first is the interval in the condition, and the second field in the table? - germaine_Lang commented on March 19th 20 at 08:49
@germaine_Lang, Yes. Or Vice versa, no difference. - Reilly46 commented on March 19th 20 at 08:52
@Reilly46, thank you! - germaine_Lang commented on March 19th 20 at 08:55
@Reilly46, still somehow not that
If I add a record with dates 09.03.2019-10.03.2019 and in the database there is a record with dates 08.03.2019-09.03.2019, it is necessary to prohibit saving. But the condition is not triggered, although it should, as the new record enters the date the old - germaine_Lang commented on March 19th 20 at 08:58
@germaine_Lang, So it is not a question to the database, and the logic of your program. - Reilly46 commented on March 19th 20 at 09:01
@Reilly46, Not in the software business. I do a regular query to get the records that are included in this range.
I need to make a request to the database transmission range of dates and to record this sample - germaine_Lang commented on March 19th 20 at 09:04
@germaine_Lang,
SELECT COUNT(*)
 FROM `table`
 WHERE `date_from` <= :interval_to
 AND :interval_from <= IFNULL(`date_to`, CURDATE())
Naturally, all dates must be in SQL format, '2019-03-09'. - Reilly46 commented on March 19th 20 at 09:07
March 19th 20 at 08:42
Select * 
from 'table' 
where (date(now()) between date(date_from) and date(date_to)) 
OR (date(date_from) <= date(now()) and date_to is null))
This is the answer to quite another question. - Davin_Veum commented on March 19th 20 at 08:45
@Davin_Veumwhy? The question "So, how to make a sample?", I responded with a full request - Penelope.Frits commented on March 19th 20 at 08:48
The question was what records fall into the interval from 2019-03-12 for 2019-03-26, and not some entries include date. - Davin_Veum commented on March 19th 20 at 08:51

Find more questions by tags MySQL