2016-05-16 106 views
-2

需要關於如何在我的數據庫中的交易表中獲得期初餘額和期末餘額的幫助。非工作日的期初餘額和期末餘額

事務表

+------------------+----------+-----------+ 
| id | trans_date | debit | credit | 
+----+-------------+----------+-----------+ 
| 1 | 2016-05-09 | 200.00 | 0.00 | 
| 2 | 2016-05-11 | 0.00 | 50.00 | 
+---------------+-------------+-----------+ 

要像下面的結果。您會意識到「2016-05-10」沒有交易,但結果顯示期初餘額和期末餘額。謝謝

+-------------+--------------+-----------+-----------+------------+ 
| trans_date | open_bal  | debit  | credit |closing_bal | 
+-------------+--------------+-----------+-----------+------------+ 
| 2016-05-09 |   0.00 | 200.00 | 0.00 | 200.00  | 
| 2016-05-10 |  200.00 |  0.00 | 0.00 | 200.00  | 
| 2016-05-11 |  200.00 |  0.00 | 50.00 | 150.00  | 
+-------------+-------------+--------------+------------+---------+ 

我試過這個,但「2016-05-10」沒有顯示在結果中。

SELECT trans_date,open_balance 
FROM(SELECT s.gen_id, s.trans_id, s.trans_date, 
s.narations, s.account_code, 
s.op_balance as open_balance, 
s.debit, s.credit, s.closing_balance 
from (select t.gen_id, t.trans_id, 
t.narations, t.account_code, 
t.trans_date, t.credit, t.debit, 
@tot_debit := if(@prev_client = t.account_code, @tot_debit + t.debit,t.debit) as tot_cred, 
@tot_credit := if(@prev_client = t.account_code,@tot_credit + t.credit,t.credit) as tot_deb, 
@cur_bal := if(@prev_client = t.account_code, @tot_debit - @tot_credit,t.debit-t.credit) as closing_balance, 
(@cur_bal + t.credit) - t.debit as op_balance, @prev_client := t.account_code 
from (select * from journal WHERE account_code = 41003 
GROUP BY trans_date order by trans_date,account_code,trans_id)t, (select @prev_client:=0,@cur_bal:=0,@tot_debit:=0,@tot_credit:= 0,@open_balance:=0)r)s) g where trans_date <= '2016-05-11' 
+2

不客氣。至少在你問之前試一下。 – user3791372

+0

只需處理應用程序級代碼中缺少日期的邏輯即可 – Strawberry

回答

0

您可以使用它來選擇MySQL中的日期列表。

SELECT DATE_ADD(CURDATE() , INTERVAL -(a.n + 10 * b.n + 100 * c.n) DAY) AS `date` 
FROM (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS a 
    CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS b 
    CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS c 

如果你想在表transactions沒有出現結果日期包括,它可以與LEFT JOIN

SELECT * FROM 
    (SELECT DATE_ADD(CURDATE() , INTERVAL -(a.n + 10 * b.n + 100 * c.n) DAY) AS `date` 
    FROM (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS a 
     CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS b 
     CROSS JOIN (SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) AS c 
) AS dates 
    LEFT JOIN transactions ON trans_date = dates.date 

注來完成,這將與當前選擇日期999天過去。如果您想要更多天,可以添加另一個CROSS JOIN,並返回9 999天。