SQL中的Join Query中的列的SUM

时间:2017-10-06 11:54:45

标签: mysql sql

我有三张桌子。

fundraisers

    fundraiserId fundDescription fundName 
        14          testingfund     test      
        15          analysis        testing

fundraisingusers

     fundraisinguserId amount fundraiserId userId
        1               1000    14          12
        2               2000    14          13
        3               5000    15          14

users

    userId  firstName
     12       xyz
     13       pqr
     14       abc

我试图显示fundraisers表中所有筹款人的fundraisingusers表中的金额和user表中的用户详细信息。我正在写这样的查询。

    SELECT f.fundraiserId as fundraiserId,
    f.fundDescription as fundDescription,
    f.fundName as fundName,
    sum(fu.amount as amount),
    fu.userId as userId FROM fundraisers as f 
    left outer join fundraisingusers as fu on f.fundraiserId =
    fu.fundraiserId left outer join users as u on fu.userId =
    u.userId";

现在我能够通过基金名称,基金描述获得所有筹款活动。但是我无法获得金额。

例如在筹款用户表中,我有两行同筹款人。因此,在查询中,因为我正在给予条件ON f.fundraiserId = fu.fundraiserId它显示筹款活动,其中两次在fundraiserId 14中输出金额显示为空。可以任何人告诉我如何从筹款表中返回独特的行,如果它具有相同的筹款人数不止一次,并且还返回筹款用户表中与同一筹款人的金额之和。

我的预期输出是这样的。

    fundraiserId fundDescription fundName   amount userId
       14         testingfund      test      3000    
       15         analysis         testing   5000

我想显示所有筹款人。如果有同样的筹款人,那么我只想在添加金额字段时显示一次。在userId栏中,如果有多个用户捐赠同一个筹款人,那么同一个筹款人应该显示金额总和。

4 个答案:

答案 0 :(得分:2)

如果您正在寻找每次募捐活动的总金额,您可以这样做:

select 
  fr.fundraiserId, 
  fr.fundDescription, 
  fr.fundName, 
  sum(fru.amount) as amount
from fundraisers as fr
join fundraisingusers fru on fru.fundraiserId = fr.fundraiserId
group by
  fr.fundraiserId, 
  fr.fundDescription, 
  fr.fundName

<强>输出

|fundraiserId  |  fundDescription  |  fundName  |  amount |  
|---------------------------------------------------------|
|14            |  testingfund      |  test      |  3000   |
|15            |  analysis         |  testing   |  5000   |

请参阅SqlFiddle Demo

但是,如果您要为每个募捐活动的用户寻找总金额,那么您可以这样做:

select 
  fr.fundraiserId, 
  fr.fundDescription, 
  fr.fundName, 
  sum(fru.amount) as amount,
  u.firstName
from fundraisers as fr
join fundraisingusers fru on fru.fundraiserId = fr.fundraiserId
join users as u on u.userId = fru.userId
group by
  fr.fundraiserId, 
  fr.fundDescription, 
  fr.fundName,
  u.firstName

<强>输出

| fundraiserId | fundDescription | fundName | amount | firstName |
|----------------------------------------------------------------|
| 15           | analysis        | testing  | 5000   | abc       |
| 14           | testingfund     | test     | 2000   | pqr       |
| 14           | testingfund     | test     | 1000   | xyz       |

请参阅SqlFiddle Demo

  

我想只显示status = 1的筹款人   f.status ='1'。我必须放置此代码吗?

如果您想添加where子句,可以在group by之前添加,类似于:

select 
  fr.fundraiserId, 
  fr.fundDescription, 
  fr.fundName, 
  sum(fru.amount) as amount
from fundraisers as fr
join fundraisingusers fru on fru.fundraiserId = fr.fundraiserId
where fr.status = 1
group by
  fr.fundraiserId, 
  fr.fundDescription, 
  fr.fundName

请参见SqlFiddle Demo以及Where子句

答案 1 :(得分:0)

你需要gruop

  SELECT f.fundraiserId as fundraiserId,
    f.fundDescription as fundDescription,
    f.fundName as fundName,
    sum(fu.amount as amount),
    fu.userId as userId 
    FROM fundraisers as f 
    left outer join fundraisingusers as fu on f.fundraiserId = fu.fundraiserId 
    left outer join users as u on fu.userId =     u.userId
    group by  f.fundraiserId as fundraiserId,
    f.fundDescription as fundDescription,
    f.fundName as fundName,   
    fu.userId as userId ";

答案 2 :(得分:0)

fundraisers表加入fundraisingusers表并执行COUNT(*)

SELECT fr.fundraiserId ,fr.fundDescription ,fr.fundName,count(*) AS amount
FROM fundraisers fr
INNER JOIN fundraisingusers fau ON fr.fundraiserId =fau.fundraiserId
GROUP BY fr.fundraiserId ,fr.fundDescription ,fr.fundName  

答案 3 :(得分:0)

使用没有连接的内部查询的另一种方法,没有分组。

Select fundraiserId, fundDescription, fundName, (select sum(amount) from fundraisingusers where fundraisers.fundraiserId = fundraisingusers.fundraiserId) as amount  from fundraisers