MySQL - 计算行之间的差异

MySQL - Calculate the difference between rows

所以我正在使用 MySQL 和 PHP。尝试获取计算连续行之间差异的查询时遇到一些问题。

我需要的是获取每个通道 data_viewsdata_subdata_videos 的每一行之间的差异 URL。

如果有人能提供帮助,将不胜感激:)

这是我的 table 结构:

试试这个..如果你不想订购,只需删除 ORDER BY A.channel_url 子句..

SELECT 
  A.*,
  A.data_views - IFNULL(B.data_views, A.data_views) AS data_views_diff,
  A.data_subs - IFNULL(B.data_subs, A.data_subs) AS data_subs_diff,
  A.data_videos - IFNULL(B.data_videos, A.data_videos) AS data_videos_diff 
FROM
  my_table A 
  LEFT JOIN my_table B 
    ON B.id = 
    (SELECT 
      MIN(id) 
    FROM
      my_table C 
    WHERE C.id > A.id 
      AND C.id != A.id 
      AND C.channel_url = A.channel_url) 
ORDER BY A.channel_url;

勾选Fiddle