add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE tablename (
`id` INTEGER,
`s_id` INTEGER,
`s_date` DATETIME,
`date_from` DATETIME,
`date_to` DATETIME
);

INSERT INTO tablename
(`id`, `s_id`, `s_date`, `date_from`, `date_to`)
VALUES
('1', '33', '2021-03-09 13:36:59', '2012-03-12 00:00:00', '2022-01-01 00:00:00'),
('2', '33', '2021-03-09 13:36:59', '2018-03-12 00:00:09', '2021-02-28 00:00:00'),
('3', '33', '2021-03-09 13:36:59', '2021-03-01 00:00:00', '2021-03-31 00:00:00'),
('4', '33', '2021-03-09 13:36:59', '2021-03-07 00:00:00', '2021-03-12 00:00:00'),
('5', '33', '2021-03-09 13:36:59', '2021-04-01 00:00:00', '2023-01-01 00:00:00'),
('6', '33', '2021-03-15 18:00:00', '2012-03-12 00:00:00', '2022-01-01 00:00:00'),
('7', '33', '2021-03-15 18:00:00', '2018-03-12 00:00:09', '2021-02-28 00:00:00'),
('8', '33', '2021-03-15 18:00:00', '2021-03-01 00:00:00', '2021-03-31 00:00:00'),
('9', '33', '2021-03-15 18:00:00', '2021-03-07 00:00:00', '2021-03-12 00:00:00'),
('10', '33', '2021-03-15 18:00:00', '2021-04-01 00:00:00', '2023-01-01 00:00:00');
SELECT *
FROM tablename
ORDER BY ABS(UNIX_TIMESTAMP(s_date) - ((UNIX_TIMESTAMP(date_from) + UNIX_TIMESTAMP(date_to)) / 2))
id s_id s_date date_from date_to
4 33 2021-03-09 13:36:59 2021-03-07 00:00:00 2021-03-12 00:00:00
8 33 2021-03-15 18:00:00 2021-03-01 00:00:00 2021-03-31 00:00:00
9 33 2021-03-15 18:00:00 2021-03-07 00:00:00 2021-03-12 00:00:00
3 33 2021-03-09 13:36:59 2021-03-01 00:00:00 2021-03-31 00:00:00
10 33 2021-03-15 18:00:00 2021-04-01 00:00:00 2023-01-01 00:00:00
5 33 2021-03-09 13:36:59 2021-04-01 00:00:00 2023-01-01 00:00:00
2 33 2021-03-09 13:36:59 2018-03-12 00:00:09 2021-02-28 00:00:00
7 33 2021-03-15 18:00:00 2018-03-12 00:00:09 2021-02-28 00:00:00
1 33 2021-03-09 13:36:59 2012-03-12 00:00:00 2022-01-01 00:00:00
6 33 2021-03-15 18:00:00 2012-03-12 00:00:00 2022-01-01 00:00:00
SELECT *
FROM tablename
ORDER BY ABS(UNIX_TIMESTAMP(s_date) - ((UNIX_TIMESTAMP(date_from) + UNIX_TIMESTAMP(date_to)) / 2))
LIMIT 2
id s_id s_date date_from date_to
4 33 2021-03-09 13:36:59 2021-03-07 00:00:00 2021-03-12 00:00:00
8 33 2021-03-15 18:00:00 2021-03-01 00:00:00 2021-03-31 00:00:00