Mysql 相邻两行记录某列的差值方法
表结构:
数据:
需求:
按照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 is not null order by info.company_id) r2 on r1.company_id = r2.company_id and r1.rownum = r2.rownum - 1
结果:
相关推荐
苏康申 2020-11-13
vitasfly 2020-11-12
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11