您好,欢迎来到投聚财经。
搜索
您的当前位置:首页Mysql相邻两行记录某列的差值方法

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常用聚合函数详解
  • php+mysql开源XNA 聚合程序发布 下载
  • MySql 中聚合函数增加条件表达式的方法
  • MySQL中聚合函数count的使用和性能优化技巧
  • 利用MySQL统计一列中不同值的数量方法示例
  • Mysql无法选取非聚合列的解决方法
  • Copyright © 2019- tjwe.cn 版权所有

    违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

    本站由北京市万商天勤律师事务所王兴未律师提供法律服务