1

我有两张桌子:

|| *id* || *calendar_type* || *event_name* || *event_details* || *start_date* || *end_date* || *closed_date* || *time_start* || *time_end* ||
|| 1 || OPEN || AVAILABLE BLOCK || _NULL_ || 2013-01-01 || 2013-12-31 || 0000-00-00 || 08:00:00 || 08:59:00 ||
|| 2 || OPEN || AVAILABLE BLOCK || _NULL_ || 2013-01-01 || 2013-12-31 || _NULL_ || 09:00:00 || 09:59:00 ||
|| 3 || OPEN || AVAILABLE BLOCK || _NULL_ || 2013-01-01 || 2013-12-31 || _NULL_ || 10:00:00 || 10:59:00 ||
|| 4 || OPEN || AVAILABLE BLOCK || _NULL_ || 2013-01-01 || 2013-12-31 || _NULL_ || 13:00:00 || 13:59:00 ||

和 Appointment_Calendar 表:其中包含可用时间段和已关闭时间段的日期和时间。

|| *id* || *start_time* || *duration_min* || *customer_id* || *consignee* || *trucker_name* || *email* || *phone* || *pallet_total* || *freight_type* || *notes* || *reserved_on* || *is_cancelled* ||
|| 1 || 2013-01-22 09:00:00 || 01:00:00 || 0 ||  || Testing ||  ||  || 20 || DELIVERY || this is a test || 2013-01-22 19:15:06 || 0 ||
|| 2 || 2013-01-22 10:00:00 || 01:00:00 || 0 ||  || Trucker 2 ||   ||  || 12 || DELIVERY ||  || 2013-01-22 19:16:37 || 0 ||
|| 4 || 2013-01-23 08:00:00 || 01:00:00 || 0 ||  || Trucker 3 ||  ||  || 10 || DELIVERY ||  || 2013-01-22 20:32:18 || 0 ||

现在我的目标是编写一个查询(可能是两个),它获取所有可用块的列表(来自约会日历)并仅返回那些不在约会中(未保留)或未在特定日期关闭的时间。下面的 SQL 执行此操作,但是当我引入子查询以省略所有约会日期或时间时,请参阅注释掉的段,所有可用的约会日历块 dissapear 。我希望只显示可用的时间块

SELECT DATE('2013-01-23 08:00:00')as today,appointment_calendar.*
FROM appointment_calendar 
WHERE calendar_type='OPEN'
AND
 DATE( '2013-01-23 08:00:00') BETWEEN start_date AND end_date
AND
 DATE( '2013-01-23 08:00:00') 
  NOT IN (SELECT closed_date  FROM appointment_calendar WHERE calendar_type='CLOSED')

/*  This does not work somehow if the time is in the appointment all blocks for that day do not appear
AND
( TIMESTAMP('2013-01-23 08:00:00')  
    NOT IN  (SELECT TIMESTAMP( start_time) FROM appointments )
)
*/

有什么建议的方法吗?

4

1 回答 1

0

被注释掉的部分将始终评估为真或假。所以它要么过滤掉所有行,要么没有行。您需要以某种方式将 'TIMESTAMP('2013-01-23 08:00:00')' 替换为约会日历表中的时间戳。也许根据 start_date 和 time_start 制作时间戳?

于 2013-01-23T05:35:18.773 回答