3

本当に簡単な質問で申し訳ありません。私は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

誰かが私の問題を手伝ってくれますか?

ありがとう

4

3 に答える 3

2

Try this running total: 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;

Output:

| 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 |

Then join to table A, final query:

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)

Output:

| 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 |

Live test: http://www.sqlfiddle.com/#!2/ce765/1


UPDATE

Not dependent on column bid sorting, running total on grouping will not be impacted: 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)

Output:

| 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 |

Live test: http://www.sqlfiddle.com/#!2/6a1e6/3

于 2012-06-03T10:59:21.963 に答える
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によるサブクエリバージョンの2倍速いことを発見しました...なぜこれが起こるのか考えている人はいますか?

編集

2番目の質問への回答(以下のコメント):

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
于 2012-06-03T10:30:15.153 に答える
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
于 2012-06-03T10:21:25.440 に答える