Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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 循环或递归sql查询_Mysql_Sql_Loops_Subquery - Fatal编程技术网

Mysql 循环或递归sql查询

Mysql 循环或递归sql查询,mysql,sql,loops,subquery,Mysql,Sql,Loops,Subquery,我有一个数字和日期表,每个日期和日期不一定间隔固定。我想得到一个数字不在表中时的日期计数 我举了一个例子,日期和芝麻饼更复杂: date | chiffre 2014-09-30 | 2 2014-09-29 | 1 2014-09-28 | 2 2014-09-27 | 2

我有一个数字和日期表,每个日期和日期不一定间隔固定。我想得到一个数字不在表中时的日期计数

我举了一个例子,日期和芝麻饼更复杂:

date                      |   chiffre
2014-09-30                |        2
2014-09-29                |        1
2014-09-28                |        2
2014-09-27                |        2
2014-09-26                |        1
2014-09-25                |        2
2014-09-24                |        2
等等

我需要的数字1:

actual_number_of_real_dates_between_two_given_dates
  1
  3
多亏了戈登·林诺夫,我才有了真正的疑问

select count(n.id) as difference
from nums n inner join
     (select min(date) as d1, max(date) as d2
      from (select date from nums where chiffre=1 order by date desc limit 2) d
     ) dd
where n.date between dd.d1 and dd.d2
如何用3测试第2行?3加4等。。。不只是过去2年?无限制2
我应该使用循环吗?或者我可以不使用?

如果您试图在表中查找1之间的间隔,则应从下一个1开始:

select n.*,
       (select date
        from nums n2
        where n2.chiffre = 1 and n2.date > n.date
        order by date
        limit 1
       ) next_date
from nums n
where chiffre = 1;
然后将其用作子查询以获取所需内容:

select n.*,
       datediff(coalesce(next_date, now()), date) as days_between
from (select n.*,
             (select date
              from nums n2
              where n2.chiffre = 1 and n2.date > n.date
              order by date
              limit 1
             ) next_date
      from nums n
      where chiffre = 1
     ) n;

我试图替换为datediffnext\u date,date\u tirage为days\u,但子查询返回的错误超过1row@sam12 . . . 这两个都应该有一个限制1。是的,谢谢,我对新表的第一个条目有一个问题,下一个日期返回空值选择chiffre,从nums选择countid,其中n.next_日期不为空,n.date和n.next_日期之间的日期为差,下一个_日期从选择日期开始,chiffre,从nums n2中选择日期,其中n2.chiffre=1和n2.date>n.date按日期顺序限制1下一个\u从nums n中选择日期,其中chiffre=1 n我尝试过的日期不为空但不起作用:/我的最终目标是获得最小/最大值和平均值