表结构:

数据:

需求:
按照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
结果:

以上这篇Mysql 相邻两行记录某列的差值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
Python流行的IDE,框架,库等相关排行榜
下一篇 :
详解Vue SPA项目优化小记
-
国家级科研成果!用机器人造飞机成为可能!
2026-05-15栏目: 教程
-
一头多用,协作式应用或成为下一个市场爆发点
2026-05-15栏目: 教程
-
链闻,广东正式上线区块链财政电子票据
2026-05-15栏目: 教程
-
华为发布最新5G全系列解决方案:包含L3级自动驾驶技术
2026-05-15栏目: 教程
-
乌镇,准备好了!
2026-05-15栏目: 教程
