I have these two columns:
start_dt end_dt
2013-09-18 14:00:00 2013-09-18 16:00:00
I want to select the rows between these two dates (stored as datetime in MYSQL), so...
SELECT *
FROM task_schedule_times
WHERE `start_dt` >= "2013-09-18 14:00:00"
AND `end_dt` <= "2013-09-18 16:00:00"
This is fine, but I need to be able to return the above row if searching for any dates within start_dt and end_dt:
"2013-09-18 15:00:00" and "2013-09-18 15:30:00"
Visual representantion (the line represents a time interval):
entry1: -----------------
select: ---------
I also need to return the row if the date range supplied falls anywhere within range of the row
entry1: -----------------
select: ----------------------
or
entry1: -----------------
select: -----------------
No results should be displayed in a case like so:
entry1: -----------------
select: -------
Hopefully these creative inputs explain things better than I can with words.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `task_schedule_times` ADD INDEX `task_times_idx_start_dt` (`start_dt`);
SELECT
*
FROM
task_schedule_times
WHERE
task_schedule_times.`start_dt` >= '2013-09-18 14:00:00'
AND task_schedule_times.`end_dt` <= task_schedule_times."2013-09-18 16:00:00"