MySQL 预订查询,

标签 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 表:它保存了哪些时间 block 可用的日期和时间,以及那些已关闭的时间 block 。

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

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

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。所以它要么过滤掉所有行,要么不过滤掉任何行。您需要以某种方式用 appointment_calendar 表中的时间戳替换“TIMESTAMP('2013-01-23 08:00:00')”。也许根据 start_date 和 time_start 创建时间戳?

关于MySQL 预订查询,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14471691/

相关文章:

mysql - 在 MySQL 的表的多列中查找串联字符串

mysql - COALESCE 未按预期工作(mysql)

php - HTML & 字符代码未加载到表单文本框中 -PHP

python - 将 Tastypie REST API 投影到 python 对象中

javascript - Node.js 从 Javascript 代码内的 MySQL 查询返回 [object Object]

mysql - 如何修复此 MySQL 触发器语句?

Python/MySQL - 加载数据本地INFILE

mysql - SQL 基于外键的约束检查

php 好友表

php - mysql查询计算评分总和