我有一个这样的表:
Type | Time
1 | 234234
2 | 234235
1 | 234238
3 | 234239
4 | 234240
1 | 234242
2 | 234245
我想计算 type=1
和下一行 type=2
的所有行的数量。
例如:这里的结果是2
。
我不知道如何将 where
子句放在下一行
上。
最佳答案
您应该能够实现 user defined variables得到总数:
select count(*) Total
from
(
select type,
@row:=(case when @prev=1 and type=2 then 'Y' else 'N' end) as Seq,
@prev:=type
from yourtable, (SELECT @row:=null, @prev:=null) r
order by time, type
) src
where Seq = 'Y'
关于mysql - 根据mysql中的下一行选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728094/