我可以在mysql中循环select吗?

我可以在mysql中循环select吗?,mysql,sql,Mysql,Sql,我有一个疑问: select count(*) from Table o where (o.time between timevariable1 and addtime(timevariable1,timeincrement) and o.Date="whatever date"; 我尝试将这个查询循环10次,将timevariable1加上timeincrement 我想检索时间介于某个范围之间的行的计数(*)。 但我有10个不同的时间范围 从00:00到00:10,共10行 从00:10

我有一个疑问:

select count(*)
from Table o
where 
(o.time between timevariable1 and addtime(timevariable1,timeincrement)
and o.Date="whatever date";
我尝试将这个查询循环10次,将timevariable1加上timeincrement

我想检索时间介于某个范围之间的行的计数(*)。 但我有10个不同的时间范围

从00:00到00:10,共10行

从00:10到00:20 4行

……等等

timevariable1的第一个值和时间增量的值是在我的代码中计算出来的


我可以用mysql中的存储过程来完成吗

您可以在这样一个查询中完成此操作

select sum(time between timevariable1 and addtime(timevariable1,timeincrement)) as t1count,
       sum(time between timevariable2 and addtime(timevariable2,timeincrement)) as t2count
from Table
where Date='whatever date'

您可以在这样一个查询中完成此操作

select sum(time between timevariable1 and addtime(timevariable1,timeincrement)) as t1count,
       sum(time between timevariable2 and addtime(timevariable2,timeincrement)) as t2count
from Table
where Date='whatever date'

您可以使用派生表创建值列表,然后使用
左联接
。大概是这样的:

select tv.val1, count(*)
from (select val1 as timevariable union all
      select val2 . . . 
     ) tv left join
     Table o
     on  o.time between tv.timevariable and addtime(tv.timevariable, timeincrement) and
         o.Date = 'whatever date;

您可以使用派生表创建值列表,然后使用
左联接
。大概是这样的:

select tv.val1, count(*)
from (select val1 as timevariable union all
      select val2 . . . 
     ) tv left join
     Table o
     on  o.time between tv.timevariable and addtime(tv.timevariable, timeincrement) and
         o.Date = 'whatever date;

样本数据和期望结果将有助于解释您正在尝试做什么。样本数据和期望结果将有助于解释您正在尝试做什么。