是否有可能有一个 mySQL 查询来获取这些字段旧余额(我的费用总和)、借方(我应该在这里输入当前日期相同 ID 的总费用)、贷方(在这些查询中不多)、新余额(与借方相同)。
现在,当一天结束时,新余额将添加到旧余额中,并为当前日期形成新余额。我有我的代码,但它不计算我以前的余额。
表:rf_expenses
--------------------------------------
id_rf_expenses rf_expense_desc
--------------------------------------
1 salary
2 bonus
3 transportation
表:费用
-----------------------------------------------------------
id_expnses id_rf_expenses expense_amt expense_date
------------------------------------------------------------
1 1 100 current
2 1 100 yesterday
3 2 50 current
4 2 50 current
5 3 200 yesterday
输出:
-----------------------------------------------------------
EXPENSE OLD BALANCE DEBIT CREDIT NEW BALANCE
-----------------------------------------------------------
SALARY 100 100 0 100
BONUS 0 100 0 100
TRANSPO 200 0 0 0
这将发生在当前日期的每个查询中。当前日期之前的任何日期都将汇总在旧余额中
SELECT
r.rf_expense_desc,
COALESCE(SUM(expense_amt), 0) - COALESCE(q1.amt2, 0) OLD,
COALESCE(q1.amt2, 0) AS NEW
FROM
rf_expenses r
LEFT JOIN
expenses e ON r.id_RF_expense = e.id_rf_expense
LEFT JOIN
(SELECT
SUM(expense_amt) amt2, id_expense,
rf_expenses.id_rf_expense
FROM
rf_expenses
LEFT JOIN
expenses ON rf_expenses.id_RF_expense = expenses.id_rf_expense
WHERE
expense_date = CURDATE( )
GROUP BY
rf_expense_desc, expense_date) q1 ON r.id_rf_expense = q1.id_rf_expense
GROUP BY
rf_expense_desc