1.查询每个班级的前三名
DROP TABLE IF EXISTS `sc`; CREATE TABLE `sc` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(200) CHARACTER SET utf8 DEFAULT NULL, `class` varchar(200) CHARACTER SET utf8 DEFAULT NULL, `score` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1; insert into sc values (1,‘badkano‘,‘一年一班‘,100); insert into sc values (2,‘百度知道团长‘,‘一年一班‘,99); insert into sc values (3,‘du小短‘,‘一年一班‘,95); insert into sc values (4,‘du小小动‘,‘一年一班‘,97); insert into sc values (5,‘du小智‘,‘一年一班‘,80); insert into sc values (6,‘吕布‘,‘一年二班‘,67); insert into sc values (7,‘赵云‘,‘一年二班‘,90); insert into sc values (8,‘典韦‘,‘一年二班‘,89); insert into sc values (9,‘关羽‘,‘一年二班‘,70); insert into sc values (10,‘马超‘,‘一年二班‘,98);
result:
select * from sc t where (select count(1)+1 from sc where class=t.class and score>t.score) <= 3