日期:2014-05-18 浏览次数:20619 次
select a.id,a.value1,sum(b.value2) from table1 a left outer join table2 b on a.id=b.id where a.id in ('1','2','3') group by a.id,b.id,a.value1
select isnull(a.id,b.id) as id, isnull(a.value1,0),isnull(b.value2,0) from (select id,sum(value1) as value1 from table1 group by id) a full join (select id,sum(value2) as value2 from table2 group by id)b on a.id=b.id
------解决方案--------------------
select A.id,A.value1,B.value2 from table1 A left join (select id,sum(value2) as value2 as value2 from table2 group by id) B on A.id = B.id
------解决方案--------------------
--如果table1为基表,且id是全的,那么没必要用full join,试试下面的 select a.*,b.value2 from table1 a left join ( select id,sum(value2) value2 from table2 group by id ) b on (a.id=b.id) where a.id in ('1','2','3')