繁体   English   中英

SQL中的联接查询中列的总和

[英]SUM of a column in Join Query in 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 table.I我写的查询这样的事情。

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

现在我可以使用资金名称,fundDescriptions获得所有筹款人。但是我无法获得全部金额。

例如在fundraisingusers表中,我有两行具有相同的fundraiserId。因此在查询中,因为我给出条件f.fundraiserId = fu.fundraiserId,因此它在输出中两次显示有fundraiserId 14的筹款活动,金额显示为空。有人能告诉我如果多次具有相同的fundraiserId,我将从筹款表返回唯一行,并且还从具有相同fundraiserId的fundraisingusers表返回金额的总和。

我的预期输出是这样的。

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

我想显示所有筹款人。如果存在相同的fundraiserId,那么我只想显示一次,并添加金额字段。在userId列中,如果有多个用户为同一筹款人捐款,则同一筹款人应显示金额总和。

如果您正在寻找每个筹款人的总额,可以按照以下步骤进行:

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


但是,如果您正在寻找每个筹款人均每位用户的总金额,则可以执行以下操作:

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


我只想显示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

请参阅带有Where子句的SqlFiddle演示


你需要

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

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  

另一种使用内部查询而无需联接,不使用分组依据的方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM