dqb78642 2017-02-10 19:34
浏览 24

MYSQL如何在预定事件之间查询设定日期和时间的开放时间段?

I am attempting to create a query to list open time slots for a specific day. I have found many similar questions on here, but none of the answers seem to do the job.

The query will need to only show results between a certain time period. For instance, if I wanted to search open time slots for the a single day I would set a start date of 2017-03-17 08:00:00 (8:00am being opening time) and an end date of 2017-03-17 17:00:00 (5:00pm being the closing time for the day). It would then have to query the day and return open slots and booked appointments from my appointments table. (Note: I do not save open slots in the table or a separate table, these will need to be generated by the query).

A simplified version of my booked appointments table:

Table name: booked_appointments (before output)

+----------------------+---------------------+----------+
|      start_time      |      end_time       | duration |
+----------------------+---------------------+----------+
| 2017-03-17 10:45:00  | 2017-03-17 11:30:00 |       45 |
| 2017-03-22 09:30:00  | 2017-03-22 10:30:00 |       60 |
| 2017-03-24 08:45:00  | 2017-03-24 11:30:00 |      165 |
| 2017-03-22 11:30:00  | 2017-03-22 12:30:00 |       60 |
+----------------------+---------------------+----------+

My current MYSQL query: (I pulled this from another answer and altered for my use)

SET @timeMinimum='2017-03-17 08:00:00';
SET @timeMaximum='2017-03-17 17:00:00';

SELECT IF (d.name = 'Free', @timeMinimum, b.start_time) AS free_from,
       IF (d.name = 'Free', b.start_time, @timeMinimum := b.start_time + INTERVAL b.duration MINUTE) AS free_until,
       d.name AS Free
FROM (SELECT 1 AS place, 'Free' AS NAME UNION SELECT 2 AS place, 'Booked' AS NAME ) AS d 
INNER JOIN booked_appointments b  
HAVING free_from < free_until
UNION SELECT @timeMinimum AS free_from, @timeMaximum AS free_until, 'Free' AS Free
FROM (SELECT 1) AS d
WHERE DATE(@timeMinimum) < DATE(@timeMaximum)

ORDER BY free_from, free_until;

The query does almost everything correctly except it ignores the timeMinimum and timeMaximum I have set and it instead returns results for the entire table that ranges from 2017-03-17 through 2017-03-24. This might not be an issue once I get query working for the day but it also is ignoring the last test date that I purposefully put out of order in the table to test a real world example of data entered.

The following is output from above query:

+---------------------+---------------------+--------+
|      free_from      |     free_until      |  Free  |
+---------------------+---------------------+--------+
| 2017-03-17 08:00:00 | 2017-03-17 10:45:00 | Free   |
| 2017-03-17 10:45:00 | 2017-03-17 11:30:00 | Booked |
| 2017-03-17 11:30:00 | 2017-03-22 09:30:00 | Free   |
| 2017-03-22 09:30:00 | 2017-03-22 10:30:00 | Booked |
| 2017-03-22 10:30:00 | 2017-03-24 08:45:00 | Free   |
| 2017-03-22 11:30:00 | 2017-03-22 12:30:00 | Booked |
| 2017-03-24 08:45:00 | 2017-03-24 11:30:00 | Booked |
+---------------------+---------------------+--------+

Expected output is:

+---------------------+---------------------+--------+
|      free_from      |     free_until      |  Free  |
+---------------------+---------------------+--------+
| 2017-03-17 08:00:00 | 2017-03-17 10:45:00 | Free   |
| 2017-03-17 10:45:00 | 2017-03-17 11:30:00 | Booked |
| 2017-03-17 11:30:00 | 2017-03-17 17:00:00 | Free   |
+---------------------+---------------------+--------+
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求视频摘要youtube和ovp数据集
    • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
    • ¥15 在启动roslaunch时出现如下问题
    • ¥15 汇编语言实现加减法计算器的功能
    • ¥20 关于多单片机模块化的一些问题
    • ¥30 seata使用出现报错,其他服务找不到seata
    • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
    • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
    • ¥15 Google speech command 数据集获取
    • ¥15 vue3+element-plus页面崩溃