是否可以通过单个查询按类别和所有产品的总价格获得所有产品的总价。
以下是我按类别给出价格的查询。
SELECT SUM(price) as totalprice
FROM products
Group BY category_id
答案 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