我有2张桌子。一个叫购物,另一个叫产品。
购物包含名为“产品”,“数量”,“大小”和“单位”的列
我的其他表,产品有列,产品,价格,尺寸和单位
我想在购物清单上购买商品的总成本,并考虑购买商品的数量。为此,我首先使用了查询:
SELECT shopping.Product, Number * Price
FROM products INNER JOIN
shopping
ON products.Product = shopping.Product;
乘以购买的商品数量*产品价格。
然后我想加上Number * Price总数,所以我使用了:
SELECT shopping.Product, SUM (Number * Price)
FROM products INNER JOIN
shopping
ON products.Product = shopping.Product;
这确实给出了正确答案,但出于某种原因,它还会输出“产品”列并打印我的某个产品的名称“牛奶”并将其与输出结合在一起。我怎么能摆脱这个?
答案 0 :(得分:0)
如果您想要所有产品的总和,请不要在product
中包含select
:
SELECT SUM(Number * Price)
FROM products INNER JOIN
shopping
ON products.Product = shopping.Product;
如果您想要每种产品的总数,请使用GROUP BY
:
SELECT shopping.Product, SUM(Number * Price)
FROM products INNER JOIN
shopping
ON products.Product = shopping.Product
GROUP BY shopping.Product;
答案 1 :(得分:0)
然后生病了修改Gordon Linoff的查询
如果您仍希望对数据进行分组,但您不希望产品列执行此操作:
Select result from
(
SELECT shopping.Product, SUM(Number * Price) as result
FROM products INNER JOIN
shopping
ON products.Product = shopping.Product
GROUP BY shopping.Product;
)as x
如果您不想将它们分组,那么就不要选择它
SELECT SUM(Number * Price) as result
FROM products INNER JOIN
shopping
ON products.Product = shopping.Product