【问题标题】:How is the query to SUM joined tables?对 SUM 连接表的查询如何?
【发布时间】:2015-09-25 09:53:48
【问题描述】:

我不知道标题应该如何。考虑标题比写这个问题需要更多的时间。
重点
假设我有三个表:

//table customers 
|  ID  | Name                  |
++++++++++++++++++++++++++++++++
| 194  | PT Comro Haneut       |
| 195  | PT Kareueut Kameumeut |

//table customer's savings 
|  ID  | IDCustomer | SavingsAmount |
+++++++++++++++++++++++++++++++++++++
|   1  |    194     |    5000000    |
|   2  |    195     |     250000    |
|   3  |    195     |    2500000    |
|   4  |    194     |     125000    |
|   5  |    194     |     175000    |

//table transactions
|  ID  | IDCustomer | Amount        |
+++++++++++++++++++++++++++++++++++++
|  1   |    195     |    1000000    |
|  2   |    195     |     250000    |
|  3   |    194     |    3500000    |
|  4   |    194     |     300000    |

目标
我想将储蓄金额和交易金额相加,并将结果放在一行中,如下所示:

// expected result of the query
| IDCustomer | Savings      | Transactions  | Balance       |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   194      |    5300000   |   4800000     |    500000     |
|   195      |    2750000   |   1250000     |   1500000     |

我尝试自己构建查询,但总是失败。我得到了节省的总金额,交易量翻了一番。

谁能帮忙?

【问题讨论】:

    标签: mysql join sum


    【解决方案1】:

    查询

    select svg.id, Savings, Transactions, (Savings - Transactions) as Balance
    from
    (
      select c.id as id, sum(s.SavingsAmount) as Savings
      from customers c
      inner join savings s
      on c.id=s.idcustomer
      group by c.id
    ) svg
    inner join
    (
      select c.id as id, sum(t.amount) as Transactions
      from customers c
      inner join transactions t
      on c.id=t.idcustomer
      group by c.id
    ) trans
    on svg.id = trans.id
    ;
    

    设置

    create table customers
    (
      id integer primary key not null,
      name varchar(23) not null
    );
    
    create table savings
    (
      id integer primary key not null,
      IDCustomer integer not null,
      SavingsAmount decimal(10, 2) not null,
      foreign key ( IDCustomer ) references customers ( id )
    );
    
    create table transactions
    (
      id integer primary key not null,
      IDCustomer integer not null,
      amount decimal(10, 2) not null,
      foreign key ( IDCustomer ) references customers ( id )
    );
    
    insert into customers
    ( id, name )
    values
    ( 194  , 'PT Comro Haneut'       ),
    ( 195  , 'PT Kareueut Kameumeut' )
    ;
    
    insert into savings 
    (  id  , IDCustomer , SavingsAmount )
    values
    (   1  ,    194     ,    5000000    ),
    (   2  ,    195     ,     250000    ),
    (   3  ,    195     ,    2500000    ),
    (   4  ,    194     ,     125000    ),
    (   5  ,    194     ,     175000    )
    ;
    
    insert into transactions
    (  id  , IDCustomer , amount        )
    values
    (  1   ,    195     ,    1000000    ),
    (  2   ,    195     ,     250000    ),
    (  3   ,    194     ,    3500000    ),
    (  4   ,    194     ,     300000    )
    ;
    

    输出

    +-----+------------+--------------+------------+
    | id  | Savings    | Transactions | Balance    |
    +-----+------------+--------------+------------+
    | 194 | 5300000.00 |   3800000.00 | 1500000.00 |
    | 195 | 2750000.00 |   1250000.00 | 1500000.00 |
    +-----+------------+--------------+------------+
    

    sqlfiddle

    【讨论】:

    • 就是这样!子查询就是答案:D
    【解决方案2】:

    看起来你不需要客户表中的任何特定数据,所以只是为了优化你可以使用的结果

    SELECT savings.IDCustomer, sum(SavingsAmount) as savings,sum(Amount) as amount, sum(SavingsAmount)-sum(Amount) as Balance
    FROM savings
    LEFT JOIN transactions
    ON savings.IDCustomer=transactions.IDCustomer
    group by savings.IDCustomer
    ORDER BY savings.IDCustomer;
    

    【讨论】:

    • 是的,如果我想显示客户姓名,我只需要客户表。但是这个查询产生了与另一个答案相同的结果。
    • 同意,结果是一样的。但是通过避免在查询中加入不需要的表,可以提高查询执行时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2017-11-02
    相关资源
    最近更新 更多