Reputation: 5961
I am storing monetary values in mysql database. a credit is stored as say 20.00
while a debit is stored as -5.00. and then whew i need the current balance i so a
Select Sum(amount)
which in this case give me 15.00
example table 1
uid amt
1 20
1 -5
example table 2
uid amt type
1 20 crd
1 5 dbt
Then in the case of a history of accounts, i can just
select * from amount
Because this is monitory values, and since database systems like mysql depreciates features every now and then i am afraid if this is the right way to do things, i also thought of adding another field which may indicate the type of transaction like
crd for credit
dbt for debit
etc
what would be your suggestions/the best way to do this?
Upvotes: 0
Views: 782
Reputation: 191749
It would make a lot more sense to me to store the data as it is (i.e. be explicit) and use a negative number. Should you use an unsigned decimal and store the type, you can still do it though.
SELECT SUM(IF(type = 'dbt', -1 * amount, amount))
Upvotes: 2