select b.region_id ,sum(a.id) from a,b
where a.parent_id=c.id
and c.parent_id=b.region_id
group by b.region_id
where a.parent_id=c.id
and c.parent_id=b.region_id
group by b.region_id
调试欢乐多
select b.region_id ,sum(a.id) from a,b
where a.parent_id=c.id
and c.parent_id=b.region_id
group by b.region_id
a表中有一个字段和c表中的一个字段相对应
而c表中又可以通过一个计算公式的出b表的字段
比方说
a表中有一个字段是人员
而b表中就只有地区没有人员
但c表中又有人员,又有地区,
而我要做的就是统计一个地区一共有多少人
所以,我需要通过c表来进行两张表的关联我还有一个疑问,就是,你们写的SQL语句中没有在from 中调用这张c表,就可以运用c表中的字段了吗????
可以
select 地区,count(人员) 人员数 from c group by 地区;
如果你的c表中只存有编码,没有描述,可以如下:
select 地区名称,count(人员) 人员数 from c,b
where c.地区= b.地区 group by c.地区;如果你想即使该地区没人也要显示一个0,那么
select 地区名称,count(人员) 人员数 from c,b
where b.地区= c.地区(+) group by c.地区;