[英]MySQL Reservation Query ,
我有两个表:
|| *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 ||
现在,我的目标是编写一个查询(可能是两个查询),以获取所有可用块的列表(来自pointation_calendar),并仅返回未在约会中(未保留)或在特定日期未关闭的那些时间。 下面的SQL可以做到这一点,但是当我引入一个子查询来忽略所有约会日期或时间(见注释掉的段)时,所有可用的subscription_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 )
)
*/
有什么建议的方法吗?
被注释掉的部分将始终评估为true或false。 因此,它要么过滤掉所有行,要么不过滤任何行。 您需要以某种方式将“ TIMESTAMP('2013-01-23 08:00:00')'替换为ational_calendar表中的时间戳。 也许根据start_date和time_start做一个时间戳?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.