当前位置:编程学习 > C#/ASP.NET >>

求一条复杂的Sql语句(oracle中的)

现有a,b,c三个表
  a表         b表        c表
  AID         BID        CID
  Code        Code       BID
  name                  number
  age

a表中的Code字段和b表中的Code字段是一一对应的关系,b表中的BID和c表中的BID是一对多的关系,要求是根据a表的Code字段去b表中找出BID,然后在c表中根据这个BID将number求和,查询结果为a表的所有字段和刚才求出的number的和作为一条记录 --------------------编程问答-------------------- select a.*,e.number from a left join (select c.cid,c.bid,sum(number) as number from c where c.bid in (select a.*,b.* from a ,b where a.code = b.code) group by c.cid,c.bid) e on a.bid = e.bid

大概就是这样的吧
--------------------编程问答-------------------- (select c.cid,c.bid,sum(number) as number from c where c.bid in (select a.*,b.* from a ,b where a.code = b.code) group by c.cid,c.bid) e

应当修正并简化为
(select bid,sum(number) as number from c group by c.bid) e

另外,lz没有明确说出B表中,code与bid是否1-1关系
如果是
整个语句似乎应当为:
select a.*,e.number from a inner join b On a.code=b.code
left join (select bid,sum(number) as number from c group by c.bid) e on b.bid = e.bid --------------------编程问答-------------------- 标准答案:
SELECT a.*, e.number   
FROM  a, b,
(SELECT c.bid, SUM(number) AS number FROM c GROUP BY c.bid )AS e
WHERE a.code = b.code
AND b.bid = c.bid --------------------编程问答-------------------- 老头子贴出的是最优解决方案! --------------------编程问答-------------------- 支持老头子的。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,