UNION查询中的部分求和和减法(减号)

时间:2012-10-23 18:48:18

标签: mysql union subtraction

我有两个类似的表(一个用于账单,另一个用于支付)现在我向用户显示一个混合来自两个数据的联盟..

Table Bills                             
CustomerId  Amount                      
1           100                        
2           100                        
1           100                        
2           100

Table Payments
CustomerId  Amount
1           100
2           100
1           100

现在我的用户可以看到以下信息

From Customer 1
Type    CustomerId   Amount
B       1            100
P       1            -100
B       1            100
P       1            -100
TOTAL 0

From Customer 2
Type    CustomerId   Amount
B       1            100
P       1            -100
B       1            100
Total 100

使用UNION Statement

可以正常工作

现在我需要在每条记录上显示部分余额,以便用户在查看记录时可以记住balanca ..

像这样......(希望)

From Customer 1
Type    CustomerId   Amount    Partial
B       1            100       100
P       1            -100      0
B       1            100       100
P       1            -100      0

我已经尝试使用像@Partial这样的变量:= @Partial + Amount但似乎它首先将第一部分(账单)和其余部分(付款)相加......就像这样......

From Customer 1
Type    CustomerId   Amount    Partial
B       1            100       100
P       1            -100      200
B       1            100       200
P       1            -100      100
似乎首先从账单中总结一切,然后开始减法...谁知道如何解决它?

** * ** * //更新// * ** * ****

这里是原始查询...

(SELECT'Bill'为cType,b.type,b.tal,'Customer',b.number,b.date,b.subtot,b.tax,IF(b.type ='CA'或b .type ='CB'或b.type ='CC'或b.type ='CX',b.total * -1,b.total)as FROM FROM bill b WHERE b.idcustomer ='000140')UNION ALL (选择'付款'为cType,'CO','1','',c.idcash,c.date,0,0,-c.amount FROM cash c WHERE c.idcustomer ='000140'和(c。 type ='CO'或c.type ='DM'))按日期排序asc;

这带来了类似的东西

Bill FX 1客户9 2011-02-25 0.00 0.00 100.00

付款CO 1 37 2011-03-04 0.00 0.00 -100.00

Bill FX 1客户616 2011-03-23 0.00 0.00 100.00

付款CO 1 751 2011-04-12 0.00 0.00 -100.00

Bill FX 1客户1267 2011-04-27 0.00 0.00 100.00

付款CO 1 1157 2011-05-10 0.00 0.00 -100.00

Bill FX 1客户1974 2011-05-26 0.00 0.00 100.00

付款CO 1 1654 2011-06-08 0.00 0.00 -100.00

然后当我尝试将patiars总和时......使用以下代码

设置@ running_total = 0; (选择'Bill'为cType,b.type,b.tal,'Customer',b.number,b.date,b.subtot,b.tax,IF(b.type ='CA'或b.type = 'CB'或b.type ='CC'或b.type ='CX',b.total * -1,b.total)as total,(@ run_total:= @running_total + total)AS RunningTotal FROM bills b WHERE b.idcustomer ='000140')UNION ALL (选择'付款'为cType,'CO','1','',c.idcash,c.date,0,0,-c.amount,(@ run_total:= @ running_total-c.amount)AS RunningTotal来自现金c WHERE c.idcustomer ='000140'和(c.type ='CO'或c.type ='DM'))按日期排序asc;

结果...

Bill FX 1客户9 2011-02-25 0.00 0.00 100.00 100.00

付款CO 1 37 2011-03-04 0.00 0.00 -100.00 1905.00

Bill FX 1客户616 2011-03-23 0.00 0.00 100.00 200.00

付款CO 1 751 2011-04-12 0.00 0.00 -100.00 1805.00

Bill FX 1客户1267 2011-04-27 0.00 0.00 100.00 300.00

付款CO 1 1157 2011-05-10 0.00 0.00 -100.00 1705.00

正如你所看到的那样,似乎首先从账单中总结所有内容,然后从付款中开始实施......

1 个答案:

答案 0 :(得分:0)

以下是我能想到在MySQL中执行此操作的唯一方法,假设您有一个用于订购记录的时间戳。如果你有相当数量的数据,这可能证明效率太低:

select t.*,
       ((select sum(amount)
         from bills b
         where b.customerId = t.customerId and
               b.datetime <= t.datetime
        ) -
        (select sum(amount)
         from payments p
         where p.customerid = t.customerid and
               p.datetime <= t.datetime
        )
       ) as balance
from ((select 'B' as type, customerid, amount, datetime from bills b) union all
      (select 'P', customerid, amount, datetime from payments p)
     ) t