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