日期:2014-05-18  浏览次数:20747 次

急救:又是Join又是Sum的查询语句
三个表:

A   (id,字段1,字段2,字段3)

B     (id,字段5,字段6,字段7)

C   (id,字段8,字段9,字段10)

已知:
A表   id不重复,主键

B表
sum(字段5)   as   zje1     group   by   B.id

C表
sum(字段8)   as   zje2     group   by   C.id


请问如何显示出

(A   (id,字段1,字段2,字段3,zje1,   zje2))


------解决方案--------------------
select a.*,b.zje1,c.zje2
from tableA as a
left join
(select id,sum(字段5) as zje1 from tableB group by id) as b on a.id = b.id
left join
(select id,sum(字段8) as zje2 from tableC group by id) as c on a.id = c.id