在单个查询中按总和和总和获取组

时间:2012-03-27 17:08:09

标签: mysql

是否可以通过单个查询按类别和所有产品的总价格获得所有产品的总价。

以下是我按类别给出价格的查询。

SELECT SUM(price) as totalprice
FROM products 
Group BY category_id

2 个答案:

答案 0 :(得分:57)

在您的查询中使用ROLLUP

GROUP BY子句允许使用WITH ROLLUP修饰符,以便将额外的行添加到摘要输出中。

SELECT category_id,SUM(price) as totalprice
FROM products 
GROUP BY category_id WITH ROLLUP

答案 1 :(得分:20)

SELECT category_id, SUM(price) as totalprice
FROM products 
GROUP BY category_id WITH ROLLUP

Check out the page in the manual