共計 818 個字符,預計需要花費 3 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
本篇文章給大家分享的是有關怎么在 Mysql 中計算相鄰兩行記錄某列的差值,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。
表結構:
數據:
需求:
按照 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 中計算相鄰兩行記錄某列的差值,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。
向 AI 問一下細節正文完