使用表之间的UNION与mySQL运行平衡

时间:2016-08-27 08:20:22

标签: php mysql

需要一些帮助,我有一张桌子上的钱(收到付款)和另一张桌子上的钱(费用),这里有一个简化的表格格式:

table : money_in
+-------+-----------+-----------+------------+
|   id  | invoiceID |   amount  |   Date     |
+-------+-----------+-----------+------------+
|   1   |   120     |    1200   | 10-08-2016 |
|   2   |   120     |    1000   | 09-08-2016 |
|   3   |   453     |    750    | 05-08-2016 |
|   4   |   111     |    400    | 02-08-2016 |
|   5   |   111     |    800    | 01-08-2016 |
+-------+-----------+-----------+------------+

table : money_out
+-------+-----------+-----------+------------+
|   id  |purchaseID |   amount  |   Date     |
+-------+-----------+-----------+------------+
|   1   |   142     |    20     | 08-08-2016 |
|   2   |   142     |    50     | 07-08-2016 |
|   3   |   323     |    150    | 06-08-2016 |
|   4   |   344     |    100    | 04-08-2016 |
|   5   |   220     |    100    | 03-08-2016 |
+-------+-----------+-----------+-----------+

我创建了一个帐单表单,以便在每次进/出交易后显示运行余额。我使用查询来记录表的UNION,如下所示:

$query = "SELECT p.*, i.invoiceID AS transactionID, i.invoiceNumber AS transactionName, i.clientID, i.type AS transactionType, c.id AS entityID, c.clientName FROM " . $wpdb->prefix . 'qi_payments' . " p , " . $wpdb->prefix . 'qi_invoices'. " i, " . $wpdb->prefix . 'qi_clients'. " c WHERE p.invoiceID = i.invoiceID AND i.clientID = c.id
    UNION ALL
    SELECT pp.*, pur.purchaseID, pur.purchaseNumber, pur.supplierID, pur.type, s.id, s.supplierName FROM " . $wpdb->prefix . 'qi_purchases_payments' . " pp , " . $wpdb->prefix . 'qi_purchases'. " pur, " . $wpdb->prefix . 'qi_suppliers'. " s WHERE pp.purchaseID = pur.purchaseID AND pur.supplierID = s.id ORDER BY paymentDate DESC";

现在我有一个正确显示的表但没有最后一列(运行余额),而它应该如下所示:

+-------+-----------+-----------+------ -----+
|   In  |    Out    |  Balance  |   Date     |
+-------+-----------+-----------+------ -----+
| 1200  |           |  3730     | 10-08-2016 |
| 1000  |           |  2530     | 09-08-2016 |
|       |   20      |  1530     | 08-08-2016 |
|       |   50      |  1550     | 07-08-2016 |
|       |   150     |  1600     | 06-08-2016 |
|  750  |           |  1750     | 05-08-2016 |
|       |   100     |  1000     | 04-08-2016 |
|       |   100     |  1100     | 03-08-2016 |
|  400  |           |  1200     | 02-08-2016 |
|  800  |           |  800      | 01-08-2016 |
+-------+-----------+-----------+-----------+

我很困惑如何在SQL查询或PHP脚本中生成Balance值。

2 个答案:

答案 0 :(得分:1)

使用@变量,如下所示:

select *
  from (
    select A.*,@sum:=@sum+zsum
      from (
            select *,amount as zsum from money_in
            union all
            select *,-amount from money_out
           ) A, (select @sum:=0) B
     order by id asc
  ) A
 order by id desc

答案 1 :(得分:0)

E.g:

SELECT date
     , money_in
     , money_out
     , @i:=@i + COALESCE(money_in,0) - COALESCE(money_out,0) balance
  FROM
     ( SELECT date
            , amount money_in
            , NULL money_out
         FROM money_in
        UNION
          ALL
       SELECT date
            , NULL
            , amount 
         FROM money_out
     ) x
  JOIN 
     ( SELECT @i:=0) vars
 ORDER 
    BY date;