用mysql计算余额

时间:2012-02-22 12:07:05

标签: mysql sql running-total balance

我有一个包含以下数据的表:

ID      In       Out 
1      100.00    0.00   
2       10.00    0.00   
3        0.00   70.00    
4        5.00    0.00    
5        0.00   60.00   
6       20.00    0.00     

现在我需要一个查询,它给出了以下结果:

ID      In       Out    Balance
1      100.00    0.00   100.00
2       10.00    0.00   110.00
3        0.00   70.00    40.00
4        5.00    0.00    45.00
5        0.00   60.00   -15.00
6       20.00    0.00     5.00

是否可以使用一个查询执行此操作,而无需使用触发器或存储过程?

3 个答案:

答案 0 :(得分:16)

简短回答,是的

更长的答案,你可以使用一个变量来计算它,因为它在行中向下迭代,即

SELECT 
    `table`.`ID`,
    `table`.`In`,
    `table`.`Out`,
    @Balance := @Balance + `table`.`In` - `table`.`Out` AS `Balance`
FROM `table`, (SELECT @Balance := 0) AS variableInit
ORDER BY `table`.`ID` ASC

, (SELECT @Balance := 0) AS variableInit确保在开始之前将@Balance初始化为0。然后,对于每一行,它将@Balance设置为@Balance + In - Out,然后输出计算的值。

同样值得确定ORDER是否一致,否则Balance会根据返回行的顺序而有所不同。例如,如果你想将它命令回到前面,你可以将它用作子查询,然后外部查询处理计算值,从而确保平衡保持正确,即

SELECT
    `balanceCalculation`.`ID`,
    `balanceCalculation`.`In`,
    `balanceCalculation`.`Out`,
    `balanceCalculation`.`Balance`
FROM (
    SELECT 
        `table`.`ID`,
        `table`.`In`,
        `table`.`Out`,
        @Balance := @Balance + `table`.`In` - `table`.`Out` AS `Balance`
    FROM `table`, (SELECT @Balance := 0) AS variableInit
    ORDER BY `table`.`ID` ASC
) AS `balanceCalculation`
ORDER BY `balanceCalculation`.`ID` DESC

答案 1 :(得分:3)

最简单的答案是:

SELECT `ID`, 
       `In`, 
       `Out`, 
       @running_bal := @running_bal + (`In` - `Out`)  as `Balance`
FROM   tableName, (SELECT @running_bal := 0) tempName

答案 2 :(得分:0)

一个简单的LEFT JOIN就足够了:

SELECT t.ID, t.In, t.Out, (SUM(t2.In) - SUM(t2.Out)) Balance
FROM mytable t
    LEFT JOIN mytable t2 ON b2.ID <= b.ID
GROUP BY b.ID

或子查询(事实证明它的速度大约是原来的两倍)

SELECT t.ID, t.In, t.Out,
    (SELECT SUM(t2.In) - SUM(t2.Out) FROM mytable t2 WHERE t2.ID <= t.ID) Balance
FROM mytable t;