Suppose I have the following table
+----------+------------+-----------+----------+
| class_id | teacher_id | starts_at | ends_at |
+----------+------------+-----------+----------+
| 39 | 5 | 15:00:00 | 21:00:00 |
| 40 | 20 | 18:00:00 | 21:00:00 |
| 41 | 59 | 18:00:00 | 21:00:00 |
| 42 | 21 | 18:30:00 | 20:00:00 |
| 43 | 80 | 18:30:00 | 21:00:00 |
| 44 | NULL | 17:30:00 | 21:00:00 |
| 45 | 140 | 17:30:00 | 20:00:00 |
| 46 | 123 | 18:30:00 | 21:00:00 |
+----------+------------+-----------+----------+
For a particular instance, I do not have any teacher available in hand but I need to select those teacher whose class timings do not fall under a certain given time. For example. A new class (class_id=47) has been announced with a timing of 15:00:00
to 17:30:00
. Now I want to select the teacher_id
's, who's existing class timings do not fall under this interval. How should I write this query?
Tried this but I was wrong:
SELECT * from classes
where time(starts_at) not between '15:00:00' and '17:30:00'
and time(ends_at) not between '15:00:00' and '17:30:00'