gpt4 book ai didi

mysql - 根据mysql中的下一行选择行

转载 作者:行者123 更新时间:2023-11-29 14:03:29 25 4
gpt4 key购买 nike

我有一个这样的表:

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'

参见SQL Fiddle with Demo

关于mysql - 根据mysql中的下一行选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com