Answer the question
In order to leave comments, you need to log in
How to compose a MYSQL query to extract records without date intersection?
It is necessary to find in the database all records for which date intervals do not have intersection with the requested interval.
Let's say the requested interval consists of start_date and end_date. Record interval data is in the date_ranges column. The problem is that data on date intervals have an inconvenient form, they are stored in this form:
"YYYY-MM-DD - YYYY-MM-DD/YYYY-MM-DD - YYYY-MM-DD/YYYY-MM-DD - YYYY- MM-DD". How to write a query to extract records without date intersection?
Example:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(6) unsigned NOT NULL,
`content` varchar(200) NOT NULL,
`date_ranges` mediumtext NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `test` (`id`, `content`, `date_ranges`) VALUES
('1', 'content1', '2018-10-30 - 2018-11-05/2018-11-12 - 2018-11-14/2019-01-15 - 2019-08-22'),
('2', 'content2', '2018-12-15 - 2018-12-17/2018-11-15 - 2018-11-22'),
('3', 'content3', '2018-09-30 - 2018-11-19/2018-11-11 - 2018-11-16/2019-01-16 - 2019-08-18/2018-10-30 - 2018-11-05'),
('4', 'content4', '2018-10-30 - 2018-11-05'),
('5', 'content5', '');
Answer the question
In order to leave comments, you need to log in
Purely in MySQL - only if you write a stored procedure, but this is not the best option.
If possible, it is better to change the base by moving the intervals to a table of the form (`id`, `content_id`, `start_date`, `end_date`).
If this is not possible, then you will have to select all the lines and analyze them in the client.
The non-intersection condition itself is simple:
end_date_1 < start_date_2 || end_date_2 < start_date_1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question