如何 table A 中的 SELECT 行与 table B 不匹配(使用时间范围,基于 table B)

How to SELECT rows from table A that do not match with table B (using time range, based from table B)

我是一名学生,试图设计一个系统,根据他们学校现有数据库给定的教授时间表,在特定时间和日期找到所有空房间。 对于每个房间,都会生成一个时间表,从 7:30:00 - 19:30:00(递增 00:30:00)到名为 'room_schedule'[=33 的 table =]. room_id 从另一个名为 'room_details' 的 table 引用了一个房间的每个时间表。另一方面, table 'room_details' 按房间链接到 table 'prof_schedule'。教授的时间表记录在现有 table 'prof_schedule'.

这是 table 的样子

    TABLE 'room_schedule'

    day    time_start  time_end    room_id
    MWF    07:00:00    07:30:00    36
    MWF    07:30:00    08:00:00    36
    MWF    08:00:00    08:30:00    36
    MWF    08:30:00    09:00:00    36
    MWF    09:00:00    09:30:00    36
    MWF    09:30:00    10:00:00    36
    MWF    07:00:00    07:30:00    37
    MWF    07:30:00    08:00:00    37
    MWF    08:00:00    08:30:00    37
    MWF    08:30:00    09:00:00    37
    MWF    09:00:00    09:30:00    37
    MWF    09:30:00    10:00:00    37

    TABLE 'room_details'
    room_id     room_name      room       
    36          Biology Room   309   
    37          Physics Room   307


    TABLE 'prof_schedule'
    sched_id   professor       Day  room    time_start  time_end   subj
    1          Jeffrey Smith   MWF  309     8:00:00     9:30:00     Biology
    2          Ben Williams    MWF  307     8:30:00     10:00:00    Physics
    

根据教授的安排,我希望空房间的结果是这样的

RESULT vacant rooms
        day    time_start  time_end    room_name
        MWF    07:00:00    07:30:00    Biology Room
        MWF    07:30:00    08:00:00    Biology Room
        MWF    09:30:00    10:00:00    Biology Room
        MWF    07:00:00    07:30:00    Physics Room
        MWF    07:30:00    08:00:00    Physics Room
        MWF    08:00:00    08:30:00    Physics Room
    

我尝试了下面的查询,但它似乎从 'room_schedule' 中删除了所有记录,其中 time_start 和 time_end 来自 'prof_schedule'

SELECT day, time_start, time_end,  room_name 
FROM room_details AS rd 
INNER JOIN room_schedule AS rs 
ON rd.room_ID=rs.room_ID 
WHERE (
   (time_start NOT IN(SELECT time_start FROM prof_schedule)) AND 
   (time_end NOT IN (SELECT time_end FROM prof_schedule)) AND 
   (rs.day NOT IN (select day from prof_schedule)) AND 
   (rs.room_id NOT IN (SELECT DISTINCT rd.room_ID from room_details AS rd 
    INNER JOIN prof_schedule AS ps on rd.room=ps.room))
  )

有人可以提出一个可以显示我提到的结果的查询吗?

P.S。 第三个 table (prof_schedule) 的设计需要保留,因为他们会将这些从现有系统导入到我正在设计的系统中。

尝试这样的事情并告诉我。

SELECT a.day, a.time_start, a.time_end, b.room_name 
FROM `room_schedule` a 
INNER JOIN `room_details` b ON a.room_id = b.room_id
INNER JOIN `prof_schedule` c ON b.room = c.room AND 
IF(a.time_end > c.time_start, a.time_start >= c.time_end, a.time_start < c.time_start)

尝试像这样使用 GROUP BY 子句

SELECT a.*, b.room_name 
FROM `room_schedule` a 
INNER JOIN `room_details` b ON a.room_id = b.room_id
INNER JOIN `prof_schedule` c ON b.room = c.room AND 
IF(a.time_end > c.time_start, a.time_start >= c.time_end, a.time_start < c.time_start)
GROUP BY a.room_id, a.time_start, a.time_end