日期:2014-05-17 浏览次数:20564 次
SELECT a.id,SUM(ISNULL(b.money,c.money) money
FROM a
LEFT JOIN b ON a.id=b.id
LEFT JOIN c ON a.id=c.id
GROUP BY a.id
select A.ID,B.amt as B金额,C.amt as C金额
from A
left join (select Aid ,SUM(amt) as amt from B group by Aid) B on A.id=B.Aid
left join (select Aid ,SUM(amt) as amt from C group by Aid) C on A.id=C.Aid
SELECT a.*, [money] = COALESCE(a.[money], b.[money], 0) --COALESCE函数中可以加任意多的参数,它返回第一个不为NULL的参数值
FROM tableA a
LEFT JOIN tableB b
ON a.id = b.id
LEFT JOIN tableC c
ON a.id = c.id