mysql获取连续时间间隔,MySQL:连续时间内可用的时间跨度

I have a table with consecutive times and dates for an agenda.

ID date begin_time end_time

1 05-02-15 19:00:00 19:05:00

2 05-02-15 19:05:00 19:10:00

3 05-02-15 19:10:00 19:15:00

4 05-02-15 19:15:00 19:20:00

5 05-02-15 19:20:00 19:25:00

6 05-02-15 19:25:00 19:30:00

7 05-02-15 19:30:00 19:35:00

8 05-02-15 19:35:00 19:40:00

9 06-02-15 19:00:00 19:05:00

10 06-02-15 19:05:00 19:10:00

11 06-02-15 19:10:00 19:15:00

12

13

14 06-02-15 19:25:00 19:30:00

15 06-02-15 19:30:00 19:35:00

16 06-02-15 19:35:00 19:40:00

As you can see on 05-02-15 the time from 19:00 until 19:40 is consecutive

As you can see on 06-02-15 the time from 19:00 until 19:15 is consecutive

As you can see on 06-02-15 the time from 19:25 until 19:40 is consecutive

The begin time and end time always have a 5 minute difference.

I want to have all the dates, that have a consecutive timespan of x minutes. So when x = 30, the result is:

05-02-15

when x = 10, the result is:

05-02-15

06-02-15

Idea for an approach

Maybe the first step is to get all the consecutive parts, secondly count the number of records in a part (when x = 30, we need at least 30 min. / 5 min. = 6).

解决方案

This query check if you have @x/5 free slots in next @x minutes. And if so, than them cover whole @x minutes interval, means them are consecutive.

set @x=15;

select distinct t1.date

from

`agenda_specialists` as t1 join

`agenda_specialists` as t2 on

t2.date=t1.date and

t2.begin>=t1.begin and

t2.begin

group by t1.id

having count(*)=@x/5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值