2017-07-27 154 views
0

我有三張表。在SQL Server 2008中加入三張表

一個

ID NAME 
--- ---- 
1 abc 
2 asd 
3 qwe 

ID INCOME 
--- ------ 
1  2 
2  3 
1  4 

Ç

NAME TOTAL 
---- ----  
abc 8 
asd 20 

我想加入與這三個表SQL查詢生成輸出如

ID INCOME TOTAL 
--------------- 
1 6  8 
2 3  20 

任何人都可以幫助我嗎?

+0

的可能的複製(https://stackoverflow.com/questions/10195451/sql-inner-join-with-3 -tables) –

回答

0

你可以試試這個:[?SQL有3個表內加入]

select 
    t1.id, sum(t2.income) income, sum(t3.total) total 
from 
    table1 t1 
join 
    table2 t2 on t1.id = t2.id 
join 
    table3 t3 on t3.name = t1.name 
group by 
    t1.id 
order by 
    t1.id 
+0

謝謝你的幫助... – user3513024