Mysql 相邻两行记录某列的差值方法
更新时间:2018年07月03日 11:10:49 作者:chenghaoyi6
今天小编就为大家分享一篇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
结果:

以上这篇Mysql 相邻两行记录某列的差值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
MySQL性能参数详解之Skip-External-Locking参数介绍
MySQL的配置文件my.cnf中默认存在一行skip-external-locking的参数,即跳过外部锁定。根据MySQL开发网站的官方解释,External-locking用于多进程条件下为MyISAM数据表进行锁定2016-05-05


最新评论