简体   繁体   English

根据先前行循环上的计算更新行

[英]Updating Row Based on computation on Previous Row Loop

I want to update the rows the rows of different person that has wrong amount. 我想更新行数错误的其他人的行。

Num | Name| Date Paid  | pay |old | new
1   | jon | 2014-08-01 | 100 |150 | 250
2   | jon | 2014-08-02 | 50  |250 | 300
3   | jon | 2014-08-03 | 110 |300 | 410
4   | jon | 2014-08-07 | 60  |410 | 470
5   | jon | 2014-08-09 | 200 |410 | 470
6   | jon | 2014-08-10 | 10  |410 | 470
7   | jon | 2014-08-15 | 100 |410 | 470
8   | jon | 2014-08-17 | 20  |410 | 470

sample database is here 示例数据库在这里

The formula to get the amount of column(new) is that in the first date which is the number 1 is column(pay) + column(old) = column(new): 获取column(new)数量的公式是在第一个日期(即数字1)是column(pay)+ column(old)= column(new):

100+150 = 250. 

then the 250 will be put in the second row in column in Old then so on. 然后将250放在“旧”栏中的第二行中,依此类推。

column(pay) + column(old) = column(new): 50 + 250 = 300. 

I want to update all the data that is wrong. 我想更新所有错误的数据。 Is there a way to update it will for loop or anything? 有没有一种方法可以将它更新为循环? there are about 400+ data that is wrong thanks for your time and help... :D 大约有400多个错误的数据,感谢您的时间和帮助...:D

The formula of column(old) is the value of previous column(new) so the row number 5 - 8 has wrong data the column(old) and column(new) is there a way to update it continuesly? column(old)的公式是前一column(new)的值,因此行号5-8的数据错误column(old)和column(new)是否有办法连续更新它?

You can do this with this statement: 您可以使用以下语句执行此操作:

UPDATE
    person p
INNER JOIN (
    SELECT
        p1.*, 
        p2.personID as p2id, 
        p2.oldpaidamount as old2, 
        p2.newpaidamount as new2
    FROM
        person p1
    INNER JOIN 
        person p2
    ON
        p1.personID > p2.personID
    AND
        p1.personName = p2.personName
    AND
        p2.personID = (
            SELECT 
                MAX(p3.personID)
            FROM
                person p3
            WHERE
                p3.personID < p1.personID
        )
    ) temp
ON
    p.personID = temp.personID
AND
    p.personName = temp.personName
SET
    p.newpaidAmount = temp.new2 + p.paidamount,   -- add this line to correct the newpaidamount
    p.oldpaidAmount = temp.new2
WHERE
    p.oldpaidAmount <> temp.new2

We materialize our subselect on the same table to avoid the restriction of MySQL that one cannot update a table while selecting from the same in a subselect. 我们在同一表上实现子选择,以避免MySQL的限制,即MySQL无法在从子选择的同一表中进行选择时更新表。 I use an INNER JOIN to get the desired value, because the first row doesn't need to be updated. 我使用INNER JOIN来获取所需的值,因为第一行不需要更新。

See it working in the modified fiddle . 看到它在修改过的小提琴中起作用。

Note: 注意:
You can't use an UPDATE trigger to keep this current, because an UPDATE trigger would trigger itself, so you should do updates with a stored procedure. 您不能使用UPDATE触发器来保持最新状态,因为UPDATE触发器会自行触发,因此您应该使用存储过程进行更新。 For INSERT operation you could use a BEFORE INSERT trigger to get the correct value of OldpaidAmount. 对于INSERT操作,您可以使用BEFORE INSERT触发器来获取OldpaidAmount的正确值。

I had the impression that both columns are redundant, if the whole history of paidamounts is stored (here's the start one with 150 missing). 我的印象是,如果存储了payamounts的整个历史记录,那么这两列都是多余的(这是缺少150的起始列)。

    for($i=1;$i<=$totalnumberRows;$i++)
{

$mysqlUpdate = mysqli_query($con, "UPDATE
    person p
INNER JOIN (
    SELECT
        p1.*, 
        p2.personID as p2id, 
        p2.paidamount as paid2, 
        p2.oldpaidamount as old2, 
        p2.newpaidamount as new2
    FROM
        person p1
    INNER JOIN 
        person p2
    ON
        p1.personID > p2.personID
    AND
        p1.personName = p2.personName
    AND
        p2.personID = (
            SELECT 
                MAX(p3.personID)
            FROM
                person p3
            WHERE
                p3.personID < p1.personID 
                AND p3.personName = 'jon'
        )
    WHERE p1.personName = 'jon'

    ) temp
ON
    p.personID = temp.personID
AND
    p.personName = temp.personName
SET
    p.oldpaidAmount = temp.new2,
    p.newpaidAmount = temp.new2 + p.paidAmount");

}

The totalnumberRows will loop on how many times it will update until the last of the rows.. Credits to @VMai totalnumberRows将循环更新直到最后一行的更新次数。.感谢@VMai

Here is the code in sqlfiddle LINK!! 这是sqlfiddle LINK中的代码 It has a lot of Update query because i do not now how to loop in sqlfiddle but in php you can use forloop to do that thanks... 它有很多Update查询,因​​为我现在不知道如何在sqlfiddle中循环,但是在php中可以使用forloop来做到这一点……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM