select A.*,isnull(B.temcol,0) from 表1 A left join (select id,sum(列) temcol from 表2 group by id) B on B.id=A.id
Select a.id,IsNull(sum(b.字段),0) from 表1 a left join 表2 b on a.id = b.id group by a.id
Select a.id,IsNull(sum(b.字段),0) 字段 from 表1 a left join 表2 b on a.id = b.id group by a.id
原理例子:declare @a table(a int,b int) declare @b table(a int,b int) insert @a values(1,1) insert @a values(2,2) insert @b values(1,1) insert @b values(3,3)--左: select * from @a Aa left join @b Bb on Aa.a=Bb.a --右: select * from @a Aa right join @b Bb on Aa.a=Bb.a --内 select * from @a Aa join @b Bb on Aa.a=Bb.a --外 select * from @a Aa full join @b Bb on Aa.a=Bb.a --完全 select * from @a,@b
from 表1 A left join (select id,sum(列) temcol from 表2 group by id) B
on B.id=A.id
from 表1 a left join 表2 b on a.id = b.id
group by a.id
left join 表2 b on a.id = b.id
group by a.id
declare @b table(a int,b int)
insert @a values(1,1)
insert @a values(2,2)
insert @b values(1,1)
insert @b values(3,3)--左:
select * from @a Aa left join @b Bb on Aa.a=Bb.a
--右:
select * from @a Aa right join @b Bb on Aa.a=Bb.a
--内
select * from @a Aa join @b Bb on Aa.a=Bb.a
--外
select * from @a Aa full join @b Bb on Aa.a=Bb.a
--完全
select * from @a,@b