php - store/select monetory values (credit/debit) mysql

218

I am storing monetary values in mysql database. a credit is stored as say20.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 me15.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?

194

Answer

Solution:

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))

People are also looking for solutions to the problem: Smarty_Compiler.class.php Error

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.