2016-05-21 12 views
0

私はこのクエリを持っている:mysqlでselectをループすることはできますか?

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

私はtimevariable1をインクリメント、このクエリループに10回をしようとしているがtimeincrementを聖霊降臨祭。

範囲の間の時間を持つ行のカウント(*)を取得したいと考えています。 しかし、私は時間の10の異なる範囲があります。午後12時20分、4行

....とtimevariable1のよう

上の第1の値に0:10から0:10の10行

の夜12時00分から

と私のコードで時間増分の値が計算されます。

私はそれをmysqlのストアドプロシージャで行うことはできますか?

+0

サンプルデータと望ましい結果は、あなたがやろうとしているかを説明役立つだろう。 –

答えて

0

あなたはあなたが値のリストを作成し、left joinを使用するように派生テーブルを使用することができ、この

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

のように1つのクエリでこれを行うことができます。このようなもの:

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; 
関連する問題