2016-12-26 65 views
1

我有一个查询,我想要显示debit,credit & balance。表中没有balance列。我从debit & credit计算余额。显示适当的余额笔记

我试图创建一个余额列,其中余额将被存储并从表中显示出来。但是,如果我更新或删除任何行,天平将不合适。

我在SO上找到了下面的代码。但是,当一个日期有一行时,日期的多个列无法正常工作时,它就起作用。它显示日益平衡,我想逐行显示余额。

MySQL的

SELECT 
    m.`id`, 
    m.`date`, 
    m.`credit`, 
    m.`debit`, 
    SUM(t.`credit`) - SUM(t.`debit`) AS `balance` 
FROM `cash_book` m 
    JOIN (
     SELECT 
      `id`, 
      `date`, 
      `credit`, 
      `debit` 
     FROM 
      `cash_book` 
    ) t ON t.`date` <= m.`date` 
WHERE `customer_id` = 1 
GROUP BY 
    m.`id` 
ORDER BY m.`date` ASC 

它返回的结果是这样的:

Date  Debit Credit Balance 
2016-11-27  0  2000  2000 
2016-12-02  0 500000  585000 //same result for date 2016-12-02 
2016-12-02 15000   0  585000 //same result for date 2016-12-02 
2016-12-02  0 100000  585000 //same result for date 2016-12-02 
2016-12-03 1200   0  583800 
2016-12-04 3160   0  580540 //same result for date 2016-12-04 
2016-12-04 100   0  580540 //same result for date 2016-12-04 
2016-12-05  30   0  580510 
2016-12-06  0  150  580660 

但我想要的结果是这样的:

Date  Debit Credit Balance 
2016-11-27  0  2000  2000 
2016-12-02  0 500000  502000 
2016-12-02 15000   0  487000 
2016-12-02  0 100000  587000 
2016-12-03 1200   0  585800 
2016-12-04 3160   0  582640 
2016-12-04 100   0  582540 
2016-12-05  30   0  582510 
2016-12-06  0  150  582660 
+0

请也给数据,因此这将是给你 –

+0

有你的表也是一个独特的ID字段的解决方案? –

+0

@BerndBuffen是的。我有唯一的ID字段。 – smartrahat

回答

3

是这样,你找什么呢?

SELECT 
    `id`, 
    `date`, 
    `credit`, 
    `debit`, 
    @balance := @balance + credit-debit AS balance 
FROM `cash_book` 
CROSS JOIN (SELECT @balance := 0) as init 
ORDER BY `date` ASC ; 

样品

mysql> SELECT * FROM cash_book; 
+------+------------+-------+--------+ 
| id | date  | debit | credit | 
+------+------------+-------+--------+ 
| 1 | 2016-11-27 |  0 | 2000 | 
| 2 | 2016-12-04 | 3160 |  0 | 
| 3 | 2016-12-02 | 15000 |  0 | 
| 4 | 2016-12-03 | 1200 |  0 | 
| 5 | 2016-12-05 | 30 |  0 | 
| 6 | 2016-11-29 |  0 | 10000 | 
| 7 | 2016-01-05 |  0 |  0 | 
| 8 | 2016-12-01 | 2000 |  0 | 
| 9 | 2016-11-29 | 10000 |  0 | 
| 10 | 2016-12-02 | 2000 | 100000 | 
| 11 | 2016-12-06 | 2000 | 150 | 
| 12 | 2016-12-02 | 2000 | 500000 | 
+------+------------+-------+--------+ 
12 rows in set (0,00 sec) 

mysql> SELECT 
    ->  `id`, 
    ->  `date`, 
    ->  `credit`, 
    ->  `debit`, 
    ->  @balance := @balance + credit-debit AS balance 
    -> FROM `cash_book` 
    -> CROSS JOIN (SELECT @balance := 0) as init 
    -> ORDER BY `date` ASC ; 
+------+------------+--------+-------+---------+ 
| id | date  | credit | debit | balance | 
+------+------------+--------+-------+---------+ 
| 7 | 2016-01-05 |  0 |  0 |  0 | 
| 1 | 2016-11-27 | 2000 |  0 | 2000 | 
| 6 | 2016-11-29 | 10000 |  0 | 12000 | 
| 9 | 2016-11-29 |  0 | 10000 | 2000 | 
| 8 | 2016-12-01 |  0 | 2000 |  0 | 
| 3 | 2016-12-02 |  0 | 15000 | -15000 | 
| 10 | 2016-12-02 | 100000 | 2000 | 83000 | 
| 12 | 2016-12-02 | 500000 | 2000 | 581000 | 
| 4 | 2016-12-03 |  0 | 1200 | 579800 | 
| 2 | 2016-12-04 |  0 | 3160 | 576640 | 
| 5 | 2016-12-05 |  0 | 30 | 576610 | 
| 11 | 2016-12-06 | 150 | 2000 | 574760 | 
+------+------------+--------+-------+---------+ 
12 rows in set (0,00 sec) 

mysql> 
+0

是的!这正是我正在寻找的。非常感谢你。感谢所有的帮助。 – smartrahat

0

可以在select使用子查询部分像这样

SELECT 
    m.`id`, 
    m.`date`, 
    m.`credit`, 
    m.`debit`, 
    (select sum(n.`credit`) - sum(n.`debit`) 
     from `cash_book` n 
     where n.`id` = m.`id` 
     and n.`date` <= m.`date`) balance 
FROM `cash_book` m 
WHERE `customer_id` = 1 
ORDER BY m.`date` ASC 
+0

'#1054 - '字段列表'中的未知列't.credit' 我应该在查询中使用这个子查询吗? – smartrahat

+0

对不起,固定它 – GurV

+0

查询正在计算它自己的行。 – smartrahat

0

在你的查询中你已经使用了JOIN,那将用于join两个或多个表格。这里你只使用一个表格。所以在查询内部不需要JOIN

您可以使用此下面简单的查询,

SELECT 
    m.id, 
    m.date, 
    m.credit, 
    m.debit, 
    (SELECT SUM(credit)-SUM(debit) 
    FROM`cash_book` A 
    WHERE A.date<=m.Date) 
FROM `cash_book` m WHERE m.customer_id = 1 ORDER BY m.Date ASC 
+0

查询正在计算它自己的行。 – smartrahat

+0

Yah,但输出将是相同的愿望是你 –

+0

没有'JOIN' – GurV

0

尝试此查询

select s.Date,s.Debit,s.credit,ABS(@b := @b + s.debit - s.credit) as balance from (select @b:= 0.0) as dummy cross join cash_book as s order by ID;