我可以在mysql中循环选择吗?

时间:2016-05-21 15:57:03

标签: mysql sql

我有这个问题:

select count(*)
from Table o
where 
(o.time between timevariable1 and addtime(timevariable1,timeincrement)
and o.Date="whatever date";

我试图循环这个查询10次,增加时间变量1,增加时间增量。

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

从00:00到00:10 10行

从00:10到00:20 4行

....等等

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

我可以在mysql中使用存储过程吗?

3 个答案:

答案 0 :(得分:0)

您可以在一个查询中执行此操作

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'

答案 1 :(得分:0)

您可以使用派生表创建值列表,然后使用left join。像这样:

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;

答案 2 :(得分:0)

你可以在这里找到你需要知道的一切。

MySQL: Stored Procedures

MySQL: Compound Statement Syntax