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

加上一个条件后查出来的数据不准确了!怎么回事?
SELECT   Stock.GoodsID,   Goods.GoodsName,  
  Goods.NormalPrice,   SUM(Stock.Stock)   AS   Stock,  
(SUM(Stock.Stock)   *   Goods.NormalPrice)   AS   allMoney
FROM   Stock,   Goods   WHERE   Stock.GoodsID   =   Goods.GoodsID  
GROUP   BY   Stock.GoodsID,   Goods.GoodsName,   Goods.NormalPrice,   Stock

上面这样写是对的,我只要加上一个条件,   写成:
SELECT   Stock.GoodsID,   Goods.GoodsName,  
  Goods.NormalPrice,   SUM(Stock.Stock)   AS   Stock,  
(SUM(Stock.Stock)   *   Goods.NormalPrice)   AS   allMoney
FROM   Stock,   Goods   WHERE   Stock.GoodsID   =   Goods.GoodsID   AND   Stock   <   3
GROUP   BY   Stock.GoodsID,   Goods.GoodsName,   Goods.NormalPrice,   Stock

这样写以后查出来的库存数量就不对了,怎么回事呀?!
STOCK表的结构是:
ID,GoodsID,WarehouseID,Stock

上面的STOCK表中可能会有同一种GOODSID而不同WAREHOUSEID,我觉得可能问题是出在这里.比如:
ID       GoodsID       WarehouseID       Stock
1           g1                   w1                         10
2           g1                   w2                           5

谢谢!

------解决方案--------------------
SELECT Stock.GoodsID, Goods.GoodsName,
Goods.NormalPrice, SUM(Stock.Stock) AS Stock,
(SUM(Stock.Stock) * Goods.NormalPrice) AS allMoney
FROM Stock, Goods WHERE Stock.GoodsID = Goods.GoodsID GROUP BY Stock.GoodsID, Goods.GoodsName, Goods.NormalPrice, Stock
having sum( Stock) < 3

------解决方案--------------------
SELECT Stock.GoodsID, Goods.GoodsName,
Goods.NormalPrice, SUM(Stock.Stock) AS Stock,
(SUM(Stock.Stock) * Goods.NormalPrice) AS allMoney
FROM Stock, Goods WHERE Stock.GoodsID = Goods.GoodsID
GROUP BY Stock.GoodsID, Goods.GoodsName, Goods.NormalPrice, Stock
having SUM(Stock.Stock)> 3