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

sql 语句,查询总数
一张ShopProduct表中有四个字段:ID,CreateTImes,shopID 和 leftAmount 
我要查询 Sum(leftAmount)不超过 8的 shopID 和Sum(LeftAmount)
我的语句是:select * from (select SUM(sp.LeftAmount) as Amount ,sp.ShopID as shopID from ShopProduct as sp where sp.ProductID=238 group by sp.ShopID) as shopP where shopP.Amount<=8  
请问还有没有更优化的语句。

------解决方案--------------------
SQL code
select SUM(sp.LeftAmount) as Amount ,sp.ShopID as shopID 
from ShopProduct as sp where sp.ProductID=238 group by sp.ShopID
having SUM(sp.LeftAmount)<=8

------解决方案--------------------
select SUM(sp.LeftAmount) as Amount ,sp.ShopID as shopID from ShopProduct as sp where sp.ProductID=238 group by sp.ShopID having SUM(sp.LeftAmount) < 8