gpt4 book ai didi

mysql让两行相减

转载 作者:行者123 更新时间:2023-11-29 02:19:50 28 4
gpt4 key购买 nike

原表:

+----+-------------+------------------+---------------------+
| id | playnum_sum | real_playnum_sum | create_time |
+----+-------------+------------------+---------------------+
| 1 | 100| 300| 2015-05-05 19:54:01 |
| 2 | 200| 400| 2015-05-06 19:54:01 |
| 3 | 300| 500| 2015-05-07 16:09:04 |
+----+-------------+------------------+---------------------+

我想让每两行相减,结果是这样的:

计算表:

+----+-------------+------------------+---------------------+
| id | playnum_sum | real_playnum_sum | create_time |
+----+-------------+------------------+---------------------+
| 1 | 100|100| 2015-05-06 19:54:01 (this is the second time in Origin table) |
| 2 | 100|100| 2015-05-07 16:09:04 (this is the third time in Origin table) |
+----+-------------+------------------+---------------------+

请注意创建时间。如何编写sql?

最佳答案

如果您的 ID 没有间隔,您可以通过自连接来实现:

select oprev.id, o.playnum_sum - oprev.playnum_sum,
o.real_playnum_sum - oprev.real_playnum_sum,
o.create_time
from original o join
original oprev
on o.id = oprev.id + 1;

关于mysql让两行相减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684927/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com