mysql show time slots available 和 time slots busy 来自表

标签 mysql date intervals

我有这个预订表结构

|ID|timeBooked         | duration |
|2 |2013-05-09 11:10:00| 30       |
|1 |2013-05-09 14:40:00| 15       |
|AI| timespan          | int(4)   |

duration 表示以分钟为单位的持续时间。

所以我想要的是当我返回这样的记录集

查询2013-05-09 00:00:00到23:00:00之间可用的时段

|free_from|Free_until| Free |
|00:00:00 |11:10:00  | 1
|11:10:00 |11:40:00  | 0
|11:40:00 |14:40:00  | 1
|14:40:00 |14:55:00  | 0
|14:55:00 |23:00:00  | 1

单独使用 mysql 是否可行?

最佳答案

好的,纯 MySQL - 只要您喜欢这些技巧。我需要一个初始化为要显示的时间段的“开始”的变量,通常类似于 now()。

首先只是测试数据:

create table bookingEvents 
   (id int not null primary key auto_increment,
    timeBooked datetime, 
    duration int
   );


insert into bookingEvents values (null, '2013-05-13 13:22:00', 15);
insert into bookingEvents values (null, '2013-05-13 15:10:00', 45);
insert into bookingEvents values (null, '2013-05-13 19:55:00', 30);
insert into bookingEvents values (null, '2013-05-14 03:22:00', 15);
insert into bookingEvents values (null, '2013-05-14 08:19:00', 15);

然后初始化“ slider ”:

set @timeSlider='2013-05-10 00:00:00';

然后选择:

select if (d.name = 'Free', @timeSlider, b.timeBooked) as free_from,
       if (d.name = 'Free', b.timeBooked, @timeSlider := b.timeBooked + 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) d 
inner join bookingEvents b 
having free_from < free_until
order by b.timeBooked, d.place;

结果:

+---------------------+---------------------+--------+
| free_from           | free_until          | Free   |
+---------------------+---------------------+--------+
| 2013-05-10 00:00:00 | 2013-05-13 13:22:00 | Free   |
| 2013-05-13 13:22:00 | 2013-05-13 13:37:00 | Booked |
| 2013-05-13 13:37:00 | 2013-05-13 15:10:00 | Free   |
| 2013-05-13 15:10:00 | 2013-05-13 15:55:00 | Booked |
| 2013-05-13 15:55:00 | 2013-05-13 19:55:00 | Free   |
| 2013-05-13 19:55:00 | 2013-05-13 20:25:00 | Booked |
| 2013-05-13 20:25:00 | 2013-05-14 03:22:00 | Free   |
| 2013-05-14 03:22:00 | 2013-05-14 03:37:00 | Booked |
| 2013-05-14 03:37:00 | 2013-05-14 08:19:00 | Free   |
| 2013-05-14 08:19:00 | 2013-05-14 08:34:00 | Booked |
+---------------------+---------------------+--------+

如果您有一个给定的结束时间戳,那么您必须将其预先设置为@timeMaximum

set @timeSlider='2013-05-10 00:00:00';
set @timeMaximum='2013-05-14 08:35:00';


select if (d.name = 'Free', @timeSlider, b.timeBooked) as free_from,
       if (d.name = 'Free', b.timeBooked, @timeSlider := b.timeBooked + 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 bookingEvents b 
having free_from < free_until
union select @timeSlider as free_from, @timeMaximum as free_until, 'Free' as Free
from (select 1) as d
where @timeSlider < @timeMaximum

order by free_from, free_until
;

关于mysql show time slots available 和 time slots busy 来自表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16459756/

相关文章:

javascript - 按日期对具有日期字段的对象数组进行排序

arrays - 合并重叠区间的算法

Python数据框: Calculating Confidence or Prediction Intervals Using Groupby on One Column

Mysql select - 全部来自左侧且仅匹配右侧表

php - sql中的总数量

r - 使用 lubridate 计算日期间隔内的日历天数

algorithm - 间隔列表中范围非重叠间隔的最大总和

php - 如何将 implode() 的数组元素结果插入每一行?

php - PHP中的动态行列显示

MYSQL之间不包括最大日期