0

I have a question regarding free time slot retrieval in MySQL.I have 2 tables.

 Table 1:
 TimeSlots (Defines the available timeslots in 15 minutes)

 Columns : StartTime (**Time**), EndTime(**Time**)
 Data:

 00:00:00 , 00:15:00
 00:15:00 , 00:30:00
 And so on until 23:45:00 , 00:00:00

Table 2 Scheduler (Holds the scheduled tasks)

 Columns : idScheduler (**pk**) , Room (**int, fk**), StartDateTime (**DateTime**), EndDateTime (**DateTime**)

The query I use in order to retrieve the open timeslots for a day is:

     SELECT StartTime, EndTime FROM TimeSlots WHERE 
     NOT Exists (                
            SELECT Room, StartDateTime, EndDateTime FROM Scheduler
        WHERE Room = 'Room1'
        AND DATE(StartDateTime) =  '2012-12-20'
        AND 
            (StartDateTime >= concat('2012-12-20', ' ' ,StartTime) AND
            StartDateTime <= concat('2012-12-20', ' ' ,EndTime))
            OR
            (EndDateTime >= concat('2012-12-20', ' ' ,StartTime) AND
            EndDateTime <= concat('2012-12-20', ' ' ,EndTime))
            OR
            (EndDateTime <= concat('2012-12-20', ' ' ,StartTime) AND
            StartDateTime >= concat('2012-12-20', ' ' ,EndTime))
        )

Room and StartDateTime will become paramaters in the final solution.

The problem I have that if a room is booked for example that spans multiple timeslots (Example : 1, 1, 2012-12-20 13:10:00, 2012-12-20 13:55:00)

If I run the query mentioned above it will give this output:

13:00:00 - 13:15:00
13:15:00 - 13:30:00
13:30:00 - 13:45:00
13:45:00 - 14:00:00

What am I missing here?

Thanks in advance

4

1 に答える 1

0

私はこのようなことを試してみます:

SELECT StartTime, EndTime FROM TimeSlots WHERE 
 NOT Exists (                
        SELECT Room, StartDateTime, EndDateTime FROM Scheduler
    WHERE Room = 'Room1'
    AND DATE(StartDateTime) =  '2012-12-20'
    AND 
        (StartDateTime <= concat('2012-12-20', ' ' ,EndTime) AND
        EndDateTime >= concat('2012-12-20', ' ' ,StartTime))
    )

このようにして、どの部屋の予約とも交差しないタイムスロットを求めています。

于 2012-12-21T08:54:09.627 に答える