有表
A:
a bB:
c dC:
e f查找表A所有记录
aList = select a,b from A//遍历表A所有结果
for each aItem in aList 
begin //aItem.a 为A表中其中一条的a
select sun(f) from C where e in (select c from B where d = aItem.a)end怎样写成一条语句,就是sun(f)不用每个循环都查一次DB,
怎样跟select a,b from A 并在一起

解决方案 »

  1.   

    select a.a,a.b,t.f
    from a
    join 
    (
    select b.d,sum(c.f) f
    from B
    join C on B.c=C.e
    Group by B.d
    )  t on a.a=t.d
      

  2.   

    select a.a,a.b,IsNull(t.f,0) f
    from a
    left join 
    (
    select b.d,sum(c.f) f
    from B
    join C on B.c=C.e
    Group by B.d
    )  t on a.a=t.d
      

  3.   

    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html