# pre_balance in accounting log

``````[COLOR="Blue"]id debtor debtorBalance amount creditor creditorBalance[/COLOR]
``````

I have 6 columns like the above.

(1) I invest 100 dollars to my company in cash
I like to save transaction (1) like the below.

``````[COLOR="Blue"]id debtor debtorBalance amount creditor creditorBalance[/COLOR]
(1) cash   [COLOR="Red"]100[/COLOR]          100    capital      [COLOR="Red"]100[/COLOR]

``````

Because there is no cash before transaction (1),
the value of \$pre_cash is 0,
So the debtor balance (cash balance) is 100 which is 100 (this transction) + 0 (\$pre_cash).

Because there is no capital before transaction (1),
the value of \$pre_capital is 0,
So the creditor balance (capital balance) is 100 which is 100 (this transction) + 0 (\$pre_capital).

(2) I save 60 dollars of the company cash to bank.
I like to save the transaction (2) like the below.

``````[COLOR="Blue"]id debtor debtorBalance amount creditor creditorBalance[/COLOR]
(1) cash   100          100    capital      100
(2) bank   60            [COLOR="Red"]60[/COLOR]    cash          [SIZE="5"][COLOR="Red"]40[/COLOR][/SIZE]

``````

Because there is no bank before transaction (2),
the value of \$pre_bank is 0,
So the creditor balance (bank balance) is 60 which is 60 (\$this transction) - 0 (pre_bank).

Because pre_cash in the transaction (1) is 100 before transaction (2),
the value of \$pre_cash is 100,
So the creditor balance (cash balance) is 40 which is 100(pre_cash) - 60 (\$this transction).

The main question of mine in this post is how I call \$pre_cash for saving cash balance.

For calling \$pre_cash, I try to think that I have to find the last record(ORDER BY ID DESC LIMIT 1) which has
cash in “debtor” (WHERE DEBTOR=‘cash’) or(UNION) creditor(WHERE CREDITOR=‘cash’).

The trial code below doesn’t work correctly but I hope it shows what I want.

``````[b]trial code[/b]
( select debtorBalance as preBalance from accountLog
where debtor = 'cash'
UNION
select creditorBalance as preBalance from accountLog
where creditor = 'cash')
order by id desc limit 1

``````