金库每日余额登记册

Register of daily balance of valut

我想显示价值的每日余额。例如 01.01。余额为 500 欧元和 02.01。我在 02.01 表格中添加了 300 欧元索姆余额。将是 800 欧元

我有一个代码:

$balance = 'SELECT date, SUM(suma) AS DailyBalance FROM pokladna GROUP BY `date`';
    $result = mysql_query($balance);
        while ($row = mysql_fetch_array($result)) {
            echo '<div>'.$row['date'].' <---> '.str_replace('EUR','€', money_format('%.2n', 
              $row['DailyBalance'])).'</div><br>';
        }

是return

2017-01-01  157,10 €
2017-01-02  -52,00 €
2017-01-03  241,36 €

但是我想要

2017-01-01  157,10 €
2017-01-02  107,10 € (-52€)
2017-01-03  348,46 € (+241,36€)

有人可以帮忙吗?有没有办法在不增加额外 table 的情况下做到这一点?

您可以使用相关子查询:

select date,
    (
        select SUM(suma)
        from pokladna q
        where q.date <= p.date
        ) as DailyBalance
from pokladna p
group by date