【问题标题】:How to get the difference of two consecutive rows and same column in MySQL and save it to different column in the same table?如何获取MySQL中两个连续行和同一列的差异并将其保存到同一张表的不同列中?
【发布时间】:2017-04-26 21:18:28
【问题描述】:
binmin  binmax  value   diff
-0.5    0.499   20      
0.5     1.499   30      10
1.5     2.499   45      15
2.5     3.499   56      11

我想取值的差异,即。当前 - 以前的。 binmin 和 binmax 已经有序并继续增加。 您可以使用 binmin 或 binmax 作为 id。 我想将差异插入另一个名为 diff 的空列中!

【问题讨论】:

  • 您使用的是 MySQL 还是 SQLite?适当地标记。
  • 它的 MySQL !抱歉没提!

标签: mysql sql database difference


【解决方案1】:

在 MySQL/SQLite 中获取先前的值可以通过相关子查询来完成:

select t.*,
       (value -
        (select t2.value
         from testtable t2
         where t2.binmin < t.binmin
         order by t2.binmin desc
         limit 1
        )
       ) as diff
from testtable t;

【讨论】:

  • 你的表名是什么?假设只有一个表ie.testtable //假设
  • @KuldeepSinghSidhu 。 . .您不接受答案有什么原因吗?
猜你喜欢
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
相关资源
最近更新 更多