在成绩表中,取所有课程中,排名前n的的信息
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`classs` varchar(20) DEFAULT NULL,
`cores` int(64) DEFAULT NULL,
PRIMARY KEY (`_id`),
KEY `NAME_UN` (`classs`(4))
) ENGINE=InnoDB AUTO_INCREMENT=1212122 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'tt', '10');
INSERT INTO `test` VALUES ('11', 'tt', '12');
INSERT INTO `test` VALUES ('12', 'tt', '13');
INSERT INTO `test` VALUES ('111', 'w', '1');
INSERT INTO `test` VALUES ('133', 'tt', '14');
INSERT INTO `test` VALUES ('1212', 'w', '12');
INSERT INTO `test` VALUES ('12121', 'w', '121');
INSERT INTO `test` VALUES ('121212', 'w', '121');
select t2._id,t2.classs,t2.cores,count(*) from test t1 join test t2 on t1.classs=t2.classs
where t1.cores >= t2.cores
GROUP BY t2._id,t2.classs,t2.cores
HAVING count(t2._id) <= 2 (这里的数字‘2’是你想取值的数 比如排名前2)
t2._id t2.classs t2.cores count(*)
12 tt 13 2
133 tt 14 1
12121 w 121 2
121212 w 121 2
主键 班级 分数 排名
猜你喜欢
转载自1064319393.iteye.com/blog/2368108
今日推荐
周排行