I have a system balance table "A
", where the column "amount" is a debit/credit column, like this:
id amount balance
1 100 100
2 -30 70
3 40 110
4 -20 90
If a new deposit is made of i.e. $50, the new row would have to be inserted as:
5 50 140
I could achieve this by selecting last balance:
SELECT id, balance
FROM A
ORDER BY id DESC LIMIT 1
and then inserting new row:
INSERT INTO A (amount, balance)
VALUES (50, previous_balance+50)
But, is there a way to achieve this with only one query? Would it be efficient on large databases? Thank you!