MySQL预订查询,

时间:2013-01-23 02:54:57

标签: mysql

我有两张桌子:

|| *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 ||

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

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 )
)
*/

任何建议的方法?

1 个答案:

答案 0 :(得分:0)

注释掉的部分将始终评估为true或false。所以它要么过滤掉所有行,要么没有过滤掉所有行。您需要以某种方式将'TIMESTAMP('2013-01-23 08:00:00')'替换为来自appointment_calendar表的时间戳。也许根据start_date和time_start创建一个时间戳?