日期:2014-05-19  浏览次数:20741 次

求个sql语句
table   A(int   aid,string   aname)
table   B(int   bid,int   cid,string   bname)
table   C(int   cid,string   cname)
table   D(int   did,int   aid,int     bid)

表A,B,C,D第一列都是主键

求个sql语句
当did=XXX时
select出如下的行集
(did,aname,bname,cname)

------解决方案--------------------
select d.did,a.aname,b.bname,c.cname from d
inner join a on d.aid=a.aid
inner join b on d.bid=b.bid
inner join c on c.cid=b.cid
------解决方案--------------------
select D.bid,A.aname,T.bname,T.cname
from A,D,
(
select B.bid,B.bname,C.cname
from B,C
where B.cid=C.cid
) T
where A.aid=D.aid
and B.bid=D.bid

------解决方案--------------------
select did,aname,bname,cname
from D
inner join A on D.aid=A.aid
inner join
(
select B.*,C.cname from B inner join C on B.cid=C.cid
)t
on D.bid=t.bid
=====================================
select did,sum(D.price),sum(t.price)
from D
inner join
(
select B.*,C.price from B inner join C on B.cid=C.cid
)t on D.bid=T.bid
where did=XXX
group by did
不知道要哪個price sum