GROUP BY …… WITH ROLLUP 自动汇总型分组(对 SUM 之类的聚合值进行分组后的汇总),若是多字段分组的话汇总也会是多层次的,注意这是MySQL扩展语法,不是SQL标准语法。
WITH ROLLUP前的属性名一定要在SELECT中呈现出来,如果只是GROUP BY就不一定要在SELECT中表现出来。
SELECT
pm.name AS payment_method,
SUM(p.amount) AS total
FROM payments p
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
GROUP BY pm.name WITH ROLLUP