WITH POLLUP 关键字用来在所有记录的最后加上一条记录,这条记录是上面所有记录的总和,即统计记录数量
表中数据为:
1.没有WITH ROLLUP的查询语句:
SELECT
o.user_id , sum( o.amount ) amount
FROM
order_main o
GROUP BY
o.user_id ;
查询结果:
2.有WITH ROLLUP的查询语句:
SELECT
o.user_id , sum( o.amount ) amount
FROM
order_main o
GROUP BY
o.user_id WITH ROLLUP;
查询结果:
3.可使用IFNULL()函数填充(Null)空格,如果第一个条件为null,则使用第二个条件:
SELECT
IFNULL(o.user_id,'total_amount')
, sum( o.amount ) amount
FROM
order_main o
GROUP BY
o.user_id WITH ROLLUP;
查询结果: