Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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_Sql_Stored Procedures - Fatal编程技术网

Mysql 如何计算同一列中变量的日差?

Mysql 如何计算同一列中变量的日差?,mysql,sql,stored-procedures,Mysql,Sql,Stored Procedures,我有这样的数据: select * from date_table; startdate 2018-08-22 2018-08-24 2018-08-27 2018-08-29 2018-08-31 2018-09-05 2018-09-07 2018-09-10 我写了这个查询,它只给出了天的差异 CREATE temporary TABLE if not exists results AS ( select t.startdate, datediff( (select min(t1.s

我有这样的数据:

select * from date_table;
 startdate
2018-08-22
2018-08-24
2018-08-27
2018-08-29
2018-08-31
2018-09-05
2018-09-07
2018-09-10
我写了这个查询,它只给出了天的差异

CREATE temporary TABLE if not exists results AS (
select t.startdate, datediff( 
(select min(t1.startdate) 
from 
date_table t1 where
 t1.startdate>t.startdate),
t.startdate ) days_diff 
from 
date_table t) ;

select * from results:
上述查询的结果如下:

startdate   days_diff 
2018-08-22    2
2018-08-24    3
2018-08-27    2
2018-08-29    2
2018-08-31    5
2018-09-05    2
2018-09-07    3  
2018-09-10  
但我想要的结果是:

startdate     enddate      days_diff 
2018-08-22   2018-08-24      2
2018-08-27   2018-08-29      2
2018-08-31   2018-09-05      5
2018-09-07   2018-09-10      3
我正在使用MySQL(5.6版)。请让我知道是否有任何解决这个问题的办法。
提前感谢。

您似乎想枚举行,然后进行聚合。在8.0之前的版本中,可以使用变量。其余的则是聚合:

select min(start_date) as start_date, max(start_date) as end_date
from (select (@rn := @rn + 1) as seqnum, t.*
      from (select t.* from date_table t order by start_date) t cross join
           (select @rn := 0) params
     ) t
group by floor((seqnum - 1) / 2);

现在不可能了。记录没有允许区分开始和结束的属性。当然,您可以枚举记录(使用用户定义的变量)并通过奇偶。。。在本例中,请按1号模块2分组,谢谢。这对我有用。