CREATE TABLE `zz` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`n` varchar(20) DEFAULT NULL,
`k1` int(11) DEFAULT '0',
`k2` int(11) DEFAULT '0',
`k3` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of zz
-- ----------------------------
INSERT INTO `zz` VALUES ('1', 'n1', '1', '2', '3');
INSERT INTO `zz` VALUES ('2', 'n1', '1', '2', '3');
INSERT INTO `zz` VALUES ('3', 'n2', '11', '22', '33');
INSERT INTO `zz` VALUES ('4', 'n3', '111', '222', '333');
INSERT INTO `zz` VALUES ('5', 'n2', '11', '22', '33');
id n k1 k2 k3
----------------------------
1 n1 1 2 3
2 n1 1 2 3
3 n2 11 22 33
4 n3 111 222 333
5 n2 11 22 33
SQL语句为:
select n,sum(k1) as kk1, sum(k2) as kk2, sum(k3) as kk3
from zz
group by n
n kk1 kk2 kk3
----------------------------
n1 2 4 6
n2 22 44 66
n3 111 222 333