显示适当的平衡

时间:2016-12-26 10:25:29

标签: mysql

我有一个查询,我想要显示debitcredit& 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

4 个答案:

答案 0 :(得分:2)

这就是你要找的东西吗?

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>

答案 1 :(得分: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 

答案 2 :(得分: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

答案 3 :(得分: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;