Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 有没有办法将数据从前一行减去到当前行并保存到当前行_Mysql - Fatal编程技术网

Mysql 有没有办法将数据从前一行减去到当前行并保存到当前行

Mysql 有没有办法将数据从前一行减去到当前行并保存到当前行,mysql,Mysql,我有三个专栏: monthly fee remaining 第一行-条件: subtract fee(row 0) from monthly(row 0) and stored in remaining(row 0) subtract fee(this row) from remaining(previous row) and stored in remaining(this row) 第二排及以后-条件: subtract fee(row 0) from monthly(row

我有三个专栏:

monthly   fee   remaining
第一行-条件:

subtract fee(row 0) from monthly(row 0) and stored in remaining(row 0)
subtract fee(this row) from remaining(previous row) and stored in remaining(this row)
第二排及以后-条件:

subtract fee(row 0) from monthly(row 0) and stored in remaining(row 0)
subtract fee(this row) from remaining(previous row) and stored in remaining(this row)

我应该使用什么查询来执行此操作?我需要这方面的帮助。

在这两列上分别进行两个子查询,并在两个子查询中提供所需的信息,并在您希望以任何可能的方式执行此操作的字段上给出行号。现在,在这两个子查询上基于rownumber和rownumber+1进行左连接,现在您将在select中得到两列,并将rownumber字段和rownumber+1字段放在一边。将其他选择中的差异作为其他字段


希望你能理解。请仔细操作,并在您要执行此操作的特定列上下订单。

如果您只是在select语句中执行此操作,则代码实际上会是这样的

select @recordnumber:=@recordnumber+1 as recordnumber,
@curremaining:=if(@curremaining=0,Monthly,@remaining) prevremaining,
Monthly, Fee, @remaining:=@curremaining-Fee as remaining 
from Payments,(select @remaining:=0,@curremaining:=0,@recordnumber:=0) x;
在查询记录时,您可能需要一个order by,在这种情况下,只需在sql语句末尾添加order by即可。i、 e

from Payments,(select @remaining:=0,@curremaining:=0,@recordnumber:=0) x
order by fieldtoorder;

请把你的问题说清楚并举例说明。让别人更容易帮助你。你到底想解决什么问题?你为什么要做上述工作?这可能不是解决您问题的最佳方案。您如何识别行的顺序?是否有ID列或日期列?所有这些看起来都很像Excel…这些记录是否也有ID或日期字段,或者我们是否应该从按每月、费用或剩余金额排序的列表中提取数据?如何在MySql中实现行数?对我来说有点困难。但我会尽力让你知道的。你能举例说明你的答案吗?我们可以通过设置来实现行数。变量,如select@rownumber:=0作为t,然后我们可以将其与主表交叉连接。我不想从数据库检索数据。我是在表单级别执行的,所以只想将数据从前一行的item1复制到当前行的item2。