具有相同ID的MySQL SUM

时间:2012-06-03 10:05:04

标签: mysql sql

对不起真正简单的问题,我只是学习PHP& MySQL,我已经谷歌搜索了一个多星期,但我没有找到任何答案。

我创建了一个简单的财务脚本,表格如下:

table_a
aid | value
1   | 100
2   | 50
3   | 150

table_b
bid | aid | value
1   | 1   | 10
2   | 1   | 15
3   | 2   | 5
4   | 2   | 10
5   | 3   | 25
6   | 3   | 40

我想要这样的结果

No | ID | Total | Balance
1  | 1  | 10    | 90
2  | 1  | 25    | 75
3  | 2  | 5     | 45
4  | 2  | 15    | 35
5  | 3  | 25    | 125
6  | 3  | 65    | 85

有人可以帮我解决问题吗?

由于

3 个答案:

答案 0 :(得分:2)

试试这个总计:http://www.sqlfiddle.com/#!2/ce765/1

select  
    bid as no, value,
    @rt := if(aid = @last_id, @rt + value, value) as total,
    @last_id := aid
from table_b b, (select @rt := 0 as x, @last_id := null) as vars
order by b.bid, b.aid;

输出:

| NO | VALUE | TOTAL | @LAST_ID := AID |
|----|-------|-------|-----------------|
|  1 |    10 |    10 |               1 |
|  2 |    15 |    25 |               1 |
|  3 |     5 |     5 |               2 |
|  4 |    10 |    15 |               2 |
|  5 |    25 |    25 |               3 |
|  6 |    40 |    65 |               3 |

然后加入表A,最终查询:

select x.no, x.aid, x.value, x.total, a.value - x.total as balance
from
(
  select    
    bid as no, aid, value,
    @rt := if(aid = @last_id, @rt + value, value) as total,
    @last_id := aid
  from table_b b, (select @rt := 0 as x, @last_id := null) as vars
  order by b.bid, b.aid
) as x
join table_a a using(aid)

输出:

| NO | AID | VALUE | TOTAL | BALANCE |
|----|-----|-------|-------|---------|
|  1 |   1 |    10 |    10 |      90 |
|  2 |   1 |    15 |    25 |      75 |
|  3 |   2 |     5 |     5 |      45 |
|  4 |   2 |    10 |    15 |      35 |
|  5 |   3 |    25 |    25 |     125 |
|  6 |   3 |    40 |    65 |      85 |

实时测试:http://www.sqlfiddle.com/#!2/ce765/1


<强>更新

不依赖于列出价排序,对分组的运行总计不会受到影响:http://www.sqlfiddle.com/#!2/6a1e6/3

select x.no, x.aid, x.value, x.total, a.value - x.total as balance
from
(
  select    
    @rn := @rn + 1 as no, aid, value,
    @rt := if(aid = @last_id, @rt + value, value) as total,
    @last_id := aid
  from table_b b, (select @rt := 0 as x, @last_id := null, @rn := 0) as vars
  order by b.aid, b.bid
) as x
join table_a a using(aid)

输出:

| NO | AID | VALUE | TOTAL | BALANCE |
|----|-----|-------|-------|---------|
|  1 |   1 |    10 |    10 |      90 |
|  2 |   1 |    15 |    25 |      75 |
|  3 |   1 |     7 |    32 |      68 |
|  4 |   2 |     5 |     5 |      45 |
|  5 |   2 |    10 |    15 |      35 |
|  6 |   3 |    25 |    25 |     125 |
|  7 |   3 |    40 |    65 |      85 |

实时测试:http://www.sqlfiddle.com/#!2/6a1e6/3

答案 1 :(得分:1)

SELECT
   tb.bid as No,
   ta.aid as ID,
   tb.value as Total,
   ta.value-tb.total as Balance
FROM
  table_a AS ta
  INNER JOIN (
    SELECT
      tbx.aid AS aid,
      tbx.bid AS bid,
      tbx.value AS value,
      SUM(tby.value) AS total
    FROM 
      table_b AS tbx
      INNER JOIN table_b AS tby ON tby.aid=tbx.aid AND tby.bid<=tbx.bid
    GROUP BY tbx.bid
    ORDER BY tbx.bid
  ) AS tb ON tb.aid=ta.aid
ORDER BY tb.bid

正如@Quassnoi指出的那样,MySQL的效率不高。我试图使用一个怪异的连接而不是一个子查询,因为内部查询本身可能是有用的。

修改

对此感兴趣并发现连接版本的速度是@Quassnoi的子查询版本的两倍......任何人都知道为什么会这样?

修改

回答第二个问题(在下面的评论中):

SELECT
  table_a.aid AS aid,
  SUM(table_b.value) AS Total,
  table_a.value-SUM(table_b.value) AS Balance
FROM
  table_a
  INNER JOIN table_b ON table_a.aid=table_b.aid
GROUP BY table_a.aid

答案 2 :(得分:0)

您正在寻找分析功能。不幸的是,MySQL缺少它们。

您可以用效率较低的方式实现它:

SELECT  bid, aid, total, value - total
FROM    (
        SELECT  b.bid, b.aid, COALESCE(a.value, 0) AS value,
                (
                SELECT  SUM(value)
                FROM    b bp
                WHERE   bp.aid = b.aid
                        AND bp.bid <= b.bid
                ) AS total
        FROM    b
        LEFT JOIN
                a
        ON      a.aid = b.aid
        ) q