我有这样的疑问:
select count(*)
from Table o
where
(o.time between timevariable1 and addtime(timevariable1,timeincrement)
and o.Date="whatever date";我尝试将此查询循环10次,使timevariable1随时间增量递增。
我想检索时间在某个范围之间的行的计数(*)。但是我有10个不同的时间范围。
从00:00到00:10 10行
从00:10到00:20 4行
....and等
在我的代码中计算了timevariable1的第一个值和时间增量的值。
我可以在mysql中使用存储过程吗?
发布于 2016-05-21 23:59:18
您可以在一个查询中完成此操作,如下所示
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'发布于 2016-05-22 00:01:52
可以使用派生表创建值列表,然后使用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;https://stackoverflow.com/questions/37365105
复制相似问题