3 回答
TA贡献1816条经验 获得超6个赞
这比你想象的更复杂。您不能只查看连续行之间的间隔,您需要跟踪选定的最后一行以识别下一行。这意味着一个迭代过程;在 SQL 中,这通常使用递归查询来实现 - MySQL 仅支持从 8.0 版本开始。
考虑:
with recursive cte as (
select value, time
from device
where time = (select min(time) from device)
union all
select d.value, d.time
from cte c
inner join device d on d.time = (
select min(d1.time) from device d1 where d1.time >= c.time + interval 10 minute
)
)
select * from cte
TA贡献1911条经验 获得超7个赞
我建议首先使用窗口函数获取(至少)10 分钟后每行的值,然后应用递归子查询:
with recursive d(value, time, next_time) as (
select value, time,
min(time) over (order by time
range between interval 10 minute following and unbounded following
) as next_time
from device
)
recursive cte(value, time, next_time) as (
(select value, time, next_time
from d
order by time
limit 1
) union all
select d.value, d.time, d.next_time
from cte join
d
on device.time = cte.next_time
)
select *
from cte;
这是一个 db<>fiddle。
TA贡献1784条经验 获得超9个赞
我认为这会起作用:
$sql="select `value`,`time` from `device` where MINUTE(`time`) IN (0, 10, 20, 30, 40, 50) order by `time`";
如果您有 10 分钟的多个值并且只需要一个,请使用 group by MINUTE( time
)
- 3 回答
- 0 关注
- 113 浏览
添加回答
举报