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

求SQL統計語句
有如下三個表

表1   Form
字段:ID   Name   ......

表2   Device     (FormID   关联到   Form   的   ID)  
字段:ID   FormID   ......

表3:   Buy         (DeviceID   关联到   Device   的   ID)
字段:   ID   DeviceID   Number   Price   ......

求统计:  
以下记录的
select   sum(Number)   from   Buy   where   DeviceID   in(select   ID   from   Device   where   FormID   in(select   ID   from   Form   where   ID   > 10))

有没有更好的办法不用这么多的   in




------解决方案--------------------

try

Select SUM(A.Number) from Buy A
Inner Join Device B ON A.DeviceID = B.ID
Inner Join Form C ON B.FormID = C.ID
Where C.ID > 10
------解决方案--------------------
select sum(b.Number)
from Buy b,Device d,Form f
where f.ID > 10
and f.id=d.FormID
and d.id=b.DeviceID


------解决方案--------------------
select sum(Number) from Buy where DeviceID in(select ID from Device where FormID in(select ID from Form where ID > 10))


select sum(C.Number) from Buy C inner join
(select ID from Device B inner join
(select ID from Form where ID > 10) A on B.FormID = A.ID) B
on C.DeviceID = B.ID