当前位置:Gxlcms > 数据库问题 > sql求合计值(rollup函数应用)

sql求合计值(rollup函数应用)

时间:2021-07-01 10:21:17 帮助过:25人阅读

TABLE `test1` ( `id` varchar(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `count` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test2` ( `id` varchar(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

技术图片技术图片

 

-- oracle、mysql通用
select case when t2.id = 0 then 合计 else t2.id end id, sum(count) count from test1 t1,test2 t2 
where t1.id = t2.name group by t2.id;
-- mysql
select case when id is null then 合计 else id end id, sum(count) from test1 GROUP BY id with ROLLUP;

-- oracle
select id,sum(count) from test1 group by rollup(id);

 

sql求合计值(rollup函数应用)

标签:color   charset   har   acl   技术   rollup   mysq   sele   通用   

人气教程排行