[英]What would be the best way to keep a balance current in a $_Session variable
[英]What is the best way to calculate a user's current wallet balance?
我正在尝试为Web应用程序实现钱包。 我现在想出的数据库设计是一个钱包交易表,如下所示:
wallet_tr_id | user_id | amount | tr_type (debit/credit) | datetime | tr_id
应用程序中的用户可以通过现金或钱包信用进行交易。 还有另一个包含所有交易信息的transactions
表,通过外币tr_id
将通过钱包信用支付/赚取的交易信息输入到上表中。
现在,向用户显示其当前钱包余额的最佳方法是什么?
我应该为该用户计算sum total of credits - sum total of debits
,还是应该在其他表中为该用户维护current_balance
?
PS:最终对于任何用户来说,交易量都不会很大。
无需存储余额。 只需按照您说的进行计算即可:
select sum(case when tr_type = 'credit' then amount else -amount end) as balance
from transactions
where user_id = 12345;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.