makeID:2 makeID:2 BaseID:2 Date:2008-12-02 GdsID:'0005' GdsID:'0005' ... UseQua:50 StockQua:0 省略表示有其他字段 我的SQL如下: SELECT Sum(makeItems.UseQua),Sum(Stock.StockQua) FROM MakeItems INNER JOIN make ON makeItems.makeID=make.MakeID INNER JOIN Goods ON MakeItems.GdsID=Goods.GdsID INNER JOIN Stock ON Stock.GdsID=Goods.GoodsID WHERE Make.Date Between '2008-12-01' AND '008-12-02' AND Goods.GdsID='0005'
查询出来的汇总结果不对,我调试后发现有UseQua和StockQua都查了两次,各位老大帮帮我啊?
------解决方案-------------------- 你的inner join里面有1對多的情況 將inner join的條件寫全 應該可以 或者用 select sum(UseQua),sum(StockQua) from ( SELECT disitnct(makeItems.UseQua),(Stock.StockQua) FROM MakeItems INNER JOIN make ON makeItems.makeID=make.MakeID INNER JOIN Goods ON MakeItems.GdsID=Goods.GdsID INNER JOIN Stock ON Stock.GdsID=Goods.GoodsID WHERE Make.Date Between '2008-12-01' AND '008-12-02' AND Goods.GdsID='0005' )a 看看
------解决方案-------------------- 你把三个表的结构,和相关关系说说,然后要的结果是什么?
------解决方案-------------------- 好乱的3(4)个表
------解决方案-------------------- 上面的语法没有问题, 如果你要分组统计,后面要加group by加上分组统计的字段
------解决方案-------------------- 看了看,建议你先弄清楚这三表的字段连接是否会出现迪卡尔-积?
------解决方案-------------------- 问问题前,除了是找那种语句中的错误的问题, 最好不要贴自己写的逻辑乱七八早的语句,而是把需求描述和希望得到的结果贴出来。
SELECT Sum(makeItems.UseQua),Sum(Stock.StockQua)
FROM MakeItems INNER JOIN make ON makeItems.makeID=make.MakeID
INNER JOIN Goods ON MakeItems.GdsID=Goods.GdsID INNER JOIN
Stock ON Stock.GdsID=Goods.GoodsID
WHERE Make.Date Between '2008-12-01' AND '008-12-02' AND Goods.GdsID='0005'
[color=#FF0000]group by …… 进行分组[/color]
------解决方案--------------------
------解决方案--------------------
------解决方案-------------------- UP
------解决方案-------------------- 你运行下面的语句,查看一下结果集,就知道为什么聚合的数值不对了。 也许你需要加上group by子句,来指定按什么字段聚合。
SQL code
SELECT *
FROM MakeItems
INNER JOIN make ON makeItems.makeID=make.MakeID
INNER JOIN Goods ON MakeItems.GdsID=Goods.GdsID
INNER JOIN Stock ON Stock.GdsID=Goods.GoodsID
WHERE Make.Date Between '2008-12-01' AND '008-12-02' AND Goods.GdsID='0005'