表结构:

数据:

需求:
按照company_id不同分组,然后分别求出相同company_id相邻记录touch_time的差值
SQL:
select r1.company_id,
r1.touch_time,
r2.touch_time,
r1.touch_time - r2.touch_time
from (select (@rownum := @rownum + 1) as rownum,
info.company_id,
info.touch_time
from sys_touch_info info, (select @rownum := 0) r
where info.touch_time is not null
order by info.company_id) r1
left join (select (@index := @index + 1) as rownum,
info.company_id,
info.touch_time
from sys_touch_info info, (select @index := 0) r
where info.touch_time

本文介绍如何在MySQL中计算相同company_id下相邻记录的touch_time差值。通过使用变量rownum和index进行行号分配,然后通过LEFT JOIN找到相邻行并计算差值。
最低0.47元/天 解锁文章
981

被折叠的 条评论
为什么被折叠?



