SQL选择,求和和分组简单问题 [英] SQL select, sum and group simple problem

查看:114
本文介绍了SQL选择,求和和分组简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT     Product.Name, SUM(Product.Quantity) AS Balance, SUM(Income.IncomeQuantity) AS income, SUM(ProductLog.Quantity) as sold
    FROM         Income INNER JOIN
                          Product ON Income.ProductID = Product.ID INNER JOIN
                          ProductLog ON Product.ID = ProductLog.ProductID GROUP BY Name



此查询无法正常工作.在产品表中,收入为三行,该查询仅返回两行,因为productLog表仅包含两个产品的数据.我如何选择productLog或收入是否不包含当前产品ID返回0

请参阅截图 http://imageshack.us/photo/my-images/12/capturezfs.png/ [ ^ ]



This query does not works correctly. In product table and income is three rows and this query returns only two because productLog table contains data only two products. how i can select if productLog or income does not contains current product id return 0

see screenshot http://imageshack.us/photo/my-images/12/capturezfs.png/[^]

推荐答案

使用外部联接 [ ^ ]


这篇关于SQL选择,求和和分组简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆