2

我想在我的表中找到特定值的总和和计数。

在我的具体情况下,该表称为product. 表的列名是'idproduct_id和。 quantityshipping_price

该表包含:(基于SELECT * FROM product;

product_id:1    quantity:1     end_price:15     shipping_price:1
product_id:2    quantity:2     end_price:15     shipping_price:0
product_id:1    quantity:1     end_price:15     shipping_price:1
product_id:2    quantity:1     end_price:15     shipping_price:0
product_id:1    quantity:1     end_price:15     shipping_price:1
product_id:3    quantity:1     end_price:15     shipping_price:0

我需要一个 SQL 语句,它将选择具有相同 的行数,product_id得到 和 的总和。quantityend_priceshipping_price

对于此示例,我希望 SQL 语句返回项目 1:

product_id:1    quantity:3    end_price:45    shipping_price:3
4

3 回答 3

5

您可以使用SUM聚合函数,将结果按product_id.

SELECT  product_id,
        SUM(quantity) quantity,
        SUM(end_price) end_price,
        SUM(shipping_price) shipping_price
FROM    product
GROUP BY product_id
于 2012-06-14T15:24:16.110 回答
2
SELECT product_id, SUM(quantity), SUM(end_price), SUM(shipping_price) FROM product GROUP BY product_id;
于 2012-06-14T15:25:32.130 回答
0
SELECT product_id, sum(quantity), sum(end_price), sum(shipping_price) FROM product
group by product_id
于 2012-06-14T15:24:16.780 回答