I have following table for my financial system (php/mysql), with more than 5000 records. All balance fields are empty.
+---------+------+---------------------+---------+
| amount | done | date | balance |
+---------+------+---------------------+---------+
| 44.00 | 1 | 2018-02-13 14:23:32 | 0.00 |
+---------+------+---------------------+---------+
| 1000.00 | 0 | 2018-02-14 18:13:05 | 0.00 |
+---------+------+---------------------+---------+
| 500.00 | 1 | 2018-02-16 15:20:00 | 0.00 |
+---------+------+---------------------+---------+
| -40.00 | 1 | 2018-02-14 17:30:00 | 0.00 |
+---------+------+---------------------+---------+
When an user edit the field 'done' to 1, I need to update the balance field getting the amount for last payment done (based on date field).
+---------+------+---------------------+---------+
| amount | done | date | balance |
+---------+------+---------------------+---------+
| 44.00 | 1 | 2018-02-13 14:23:32 | 44.00 |
+---------+------+---------------------+---------+
| 1000.00 | 0 | 2018-02-14 18:13:05 | 0.00 |
+---------+------+---------------------+---------+
| 500.00 | 1 | 2018-02-16 15:20:00 | 544.00 |
+---------+------+---------------------+---------+
| -40.00 | 1 | 2018-02-14 17:30:00 | 4.00 |
+---------+------+---------------------+---------+
I can easily make a trigger to do it, but i cannot figure out how to update the balance for payments already done when the date of edited row is oldest (mysql don't allow run an update trigger in update context ).
Supposing that the user edit the second row, the balance of third, fourth and all following rows will updated too:
+---------+------+---------------------+---------+
| amount | done | date | balance |
+---------+------+---------------------+---------+
| 44.00 | 1 | 2018-02-13 14:23:32 | 44.00 |
+---------+------+---------------------+---------+
| 1000.00 | 1 | 2018-02-14 18:13:05 | 1004.00 |
+---------+------+---------------------+---------+
| 500.00 | 1 | 2018-02-16 15:20:00 | 1544.00 |
+---------+------+---------------------+---------+
| -40.00 | 1 | 2018-02-14 17:30:00 | 04.00 |
+---------+------+---------------------+---------+
How I can do it?