有表如下;
单独的对某一个列作sum并分组,结果如下;
对于表的第7、8行,num1都有值,num2都是null,对num2列作sum、按id分组,结果在id为4的行会显示一个null;
同时对2个列作sum,并按id分组,没有问题,结果如下;
sql
insert into mynums (id,num1,num2) values (1,13, 15)
insert into mynums (id,num1,num2) values (1,11, 17)
insert into mynums (id,num1,num2) values (2,null, 11)
insert into mynums (id,num1,num2) values (2,14,14)
insert into mynums (id,num1,num2) values (3,11, null)
insert into mynums (id,num1,num2) values (3,15,18)
insert into mynums (id,num1,num2) values (4,9,null)
insert into mynums (id,num1,num2) values (4,9,null)
select * from mynums;
select id, sum(num1) as num1 from mynums group by id;
select id, sum(num2) as num2 from mynums group by id;
select id, sum(num1) as num1, sum(num2) as num2 from mynums group by id;