是否有可能有一個mySQL查詢來獲得這些字段舊的餘額(我的開支的總和),借記卡(這裏應該把我的費用總額與當前日期相同的id),信用(沒有太多在這些查詢中),新的餘額(與借方相同)。mySQL借記貸方餘額
現在當一天結束時,新的餘額將被添加到舊的餘額中,並且將爲當前日期形成新的餘額。我有我的代碼,但它不計算我以前的餘額。
表: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
OUTPUT:
-----------------------------------------------------------
EXPENSE OLD BALANCE DEBIT CREDIT NEW BALANCE
-----------------------------------------------------------
SALARY 100 100 0 100
BONUS 0 100 0 100
TRANSPO 200 0 0 0
會發生這種情況目前date..any的每一個查詢日期以前的當前將總和在舊的餘額
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
哇..冷靜謝謝男人..救救我.. – NeverendeR