2017-07-27 86 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