MySQL 在单个查询中按总和和总和进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9894301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:44:17  来源:igfitidea点击:

Getting group by sum and total sum in a single query

mysql

提问by

Is there any way to get total price of all products by category and total price of all products in a single query.

有没有办法在单个查询中按类别获取所有产品的总价和所有产品的总价。

Below is query i am using giving price by category.

下面是我正在使用按类别给出价格的查询。

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

回答by

use ROLLUPwith your query.

ROLLUP用于您的查询。

The GROUP BY clause permits a WITH ROLLUPmodifier that causes extra rows to be added to the summary output.

GROUP BY 子句允许使用 WITHROLLUP修饰符导致将额外的行添加到摘要输出中。

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

回答by nnichols

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

Check out the page in the manual

查看手册中的页面