简体   繁体   English

MySQL保留查询,

[英]MySQL Reservation Query ,

I have two tables: 我有两个表:

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

and an Appointment_Calendar Table: Which holds the dates and times of what blocks of time are available, and those that are closed. 和一个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 ||

Now my goal is to write one query (maybe two) that gets a list of all the available blocks (from appointment_calendar ) and returns just those times that are not in appointments (not reserved) or not closed for a particular date. 现在,我的目标是编写一个查询(可能是两个查询),以获取所有可用块的列表(来自pointation_calendar),并仅返回未在约会中(未保留)或在特定日期未关闭的那些时间。 The SQL below does this but when I introduce a Subquery to omit all the appointment dates or times see commented out segment, all the available appointment_calendar blocks dissapear . 下面的SQL可以做到这一点,但是当我引入一个子查询来忽略所有约会日期或时间(见注释掉的段)时,所有可用的subscription_calendar块都会消失。 I was hoping to only have the available time blocks show 我希望只显示可用的时间段

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

Any suggested approaches? 有什么建议的方法吗?

The part that is commented out will always evaluate to either true or false. 被注释掉的部分将始终评估为true或false。 So its going to either filter out all rows, or no rows. 因此,它要么过滤掉所有行,要么不过滤任何行。 You need to somehow replace ' TIMESTAMP('2013-01-23 08:00:00')' with a timestamp from the appointment_calendar table. 您需要以某种方式将“ TIMESTAMP('2013-01-23 08:00:00')'替换为ational_calendar表中的时间戳。 Maybe make a timestamp based on start_date and time_start? 也许根据start_date和time_start做一个时间戳?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM