首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在mysql中循环select吗?

我可以在mysql中循环select吗?
EN

Stack Overflow用户
提问于 2016-05-21 23:57:03
回答 2查看 5.5K关注 0票数 1

我有这样的疑问:

代码语言:javascript
复制
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中使用存储过程吗?

EN

回答 2

Stack Overflow用户

发布于 2016-05-21 23:59:18

您可以在一个查询中完成此操作,如下所示

代码语言:javascript
复制
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'
票数 0
EN

Stack Overflow用户

发布于 2016-05-22 00:01:52

可以使用派生表创建值列表,然后使用left join。如下所示:

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37365105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档