Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:连续时间内可用的时间跨度_Mysql_Time_Timespan_Difference - Fatal编程技术网

MySQL:连续时间内可用的时间跨度

MySQL:连续时间内可用的时间跨度,mysql,time,timespan,difference,Mysql,Time,Timespan,Difference,我有一个连续时间和日期的议程表 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

我有一个连续时间和日期的议程表

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

正如您在2015年2月5日看到的,从19:00到19:40的时间是连续的

正如您在2015年2月6日看到的,从19:00到19:15的时间是连续的

正如您在2015年2月6日看到的,从19:25到19:40的时间是连续的

开始时间和结束时间总是相差5分钟

我想要所有的日期,其连续时间跨度为
x
分钟。
因此当
x=30
时,结果是:

05-02-15
x=10
时,结果为:

05-02-15
06-02-15
方法的想法


可能第一步是获取所有连续的部分,然后计算一个部分中的记录数(当x=30时,我们至少需要30分钟/5分钟=6)。

用户变量在mysql中非常有用

SELECT date,MIN(CASE WHEN BEGIN!='00:00:00' THEN BEGIN END) m,
            MAX(CASE WHEN END!='00:00:00' THEN END END) mm
FROM
  ( SELECT BEGIN,END,date, CASE
                               WHEN
                           END = BEGIN +INTERVAL 5 MINUTE THEN @n ELSE @n:=@n+1 END AS g
   FROM agenda_specialists,
     (SELECT @n:=0) x
   ORDER BY id) s
GROUP BY date, g
HAVING ((TIME_TO_SEC(mm) - TIME_TO_SEC(m))/60)>=40
本质上,你想检查开始和结束是否相等+5分钟,如果不是,你可以增加一个变量来分组,它会在不同的日期之外创建间隔。其余的很简单。我不得不更改排序规则,因为某种原因,它给了我非法的排序组合。只需使用结尾的数字即可

编辑:

您可能有
字符集\u server='latin1'

进入配置文件my.cnf,添加或取消注释以下行:

character-set-server = utf8
collation-server = utf8_general_ci

在我的机器5.5上测试此查询检查在接下来的
@x
分钟内是否有
@x/5
空闲插槽。如果是这样,那么它们覆盖整个
@x
分钟的间隔,意味着它们是连续的

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<addtime(t1.begin,sec_to_time(@x*60))
group by t1.id
having count(*)=@x/5
set@x=15;
选择不同的t1.date
从…起
`议程"专家"如t1加入
`议程(如t2所示)
t2.日期=t1.日期和
t2.开始>=t1.开始和

t2.begin

你能SQLFIDLE你的数据吗?我在MS SQL中有一个可行的答案,但我必须将窗口函数和子查询移植到mysql支持的东西上。当你使用mysql 5.6.6 m9时,你会得到一个很好的结果,但是,当您选择MySQL 5.5.32时,您会出现以下错误:
操作“=”的排序规则非法混合:当您选择MySQL 5.1.61时,您不会得到任何结果。@Daan忘了小提琴吧,您的真实单词情况如何?相同的消息?是的,我在两台服务器上测试了它(运行MySQL 5.5和5.1)我在小提琴上得到了和上面一样的错误。
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<addtime(t1.begin,sec_to_time(@x*60))
group by t1.id
having count(*)=@x/5