读书人

求SQL句解决思路

发布时间: 2012-01-19 20:57:59 作者: rapoo

求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

读书人网 >SQL Server

热点推荐