2016-05-16 122 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天。