I know this can be accomplished on the backend - but I'm wondering if there's any native, or efficient MySQL function that can be used to check if a given time
variable falls within a range covered by two time
variables (a start_time
, and an end_time
).
I have a database currently set up which looks like the following;
+----+--------+------------+----------+
| id | job_id | start_time | end_time |
+----+--------+------------+----------+
| 1 | 40 | 13:00:00 | 14:00:00 |
| 2 | 44 | 14:45:00 | 15:00:00 |
| 3 | 45 | 15:10:00 | 15:30:00 |
+----+--------+------------+----------+
The backend accepts a start_time
, and an end_time
with a job_id
- and then it looks to see if it can be fit in anywhere. So for example, given a start_time of 13:30:00
, and an end_time of 13:45:00
, the backend should reject this job request as there is no time available for it (it would overlap with the entry at id 1
.)
However, if a job is submitted with a start_time of 14:10:00
, and an end time of 14:20:00
, it should be accepted - as this does not overlap with any existing tasks.
The following query is great to tell if a job can be submitted for say, 13:00:00 until 14:00:00 (an exact duplicate of id 1);
SELECT * WHERE start_time >= '13:00:00' AND end_time <= '14:00:00';
But if the start_time becomes 13:01:00, then the query falls down - as the start_time is less than 13:01:00, at 13:00:00. So it'll get approved for insertion, as the above query will return no overlapping results.
If we change the query to an OR
clause on end_time
, then literally any job that doesn't end before 14:00:00 would be rejected.
Can anyone suggest a simple way of taking an input variable of a time
type, and checking if it falls within range of all available start_time
, and end_time
variables as noted in the db above?