求MYSQL统计的语句

liushengmz liushengmz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大家好,有一些MYSQL的数据统计麻烦一下大家。有如下表格
id gtype mtype money
1 1 1 20
2 1 1 30
3 1 2 20
4 2 0 43
5 2 0 45
6 1 0 35
7 2 4 40
8 1 5 70
9 1 2 30
10 1 0 43
11 2 1 37
12 2 2 34

MYSQL 创建 表 的语句如下:

create table `testsum` (
`id` double ,
`gtype` double ,
`mtype` double ,
`money` float
);
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('1','1','1','20');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('2','1','1','30');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('3','1','2','20');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('4','2','0','43');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('5','2','0','45');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('6','1','0','35');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('7','2','4','40');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('8','1','5','70');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('9','1','2','30');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('10','1','0','43');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('11','2','1','37');
insert into `testsum` (`id`, `gtype`, `mtype`, `money`) values('12','2','2','34');

需求:
按gtype进行分组,把mtype=0和mtype<>0的money分别求各统计出来,
分成两列,也就是结果如下
gtype mtype0money mtype2money
1 78 170
2 88 110

请大家帮帮忙。如果能用一句SQL语句就可以搞定那就最好,
如果不成,用存储过程也是可以的。

或者给我思路,让我自己去实现也行。麻烦大家了。

...全文
评论 5
收藏 已收藏
分享
返回顶部
liushengmz
疑难问题 22,298 成员 121,727 内容