-- 建表sql
CREATE TABLE `subject_score` (
`id` int(11) NOT NULL,
`score` int(255) DEFAULT NULL,
`student_id` int(11) DEFAULT NULL,
`subject_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 数据sql
BEGIN;
INSERT INTO `subject_score` VALUES (1, 21, 11, 1);
INSERT INTO `subject_score` VALUES (2, 22, 11, 2);
INSERT INTO `subject_score` VALUES (3, 23, 12, 1);
INSERT INTO `subject_score` VALUES (4, 24, 12, 2);
INSERT INTO `subject_score` VALUES (5, 25, 13, 1);
INSERT INTO `subject_score` VALUES (6, 26, 13, 2);
INSERT INTO `subject_score` VALUES (7, 27, 14, 1);
INSERT INTO `subject_score` VALUES (8, 28, 14, 2);
INSERT INTO `subject_score` VALUES (9, 29, 15, 1);
INSERT INTO `subject_score` VALUES (10, 30, 15, 2);
INSERT INTO `subject_score` VALUES (11, 31, 16, 1);
INSERT INTO `subject_score` VALUES (12, 32, 16, 2);
INSERT INTO `subject_score` VALUES (13, 33, 17, 1);
INSERT INTO `subject_score` VALUES (14, 34, 17, 2);
INSERT INTO `subject_score` VALUES (15, 35, 18, 1);
INSERT INTO `subject_score` VALUES (16, 36, 18, 2);
INSERT INTO `subject_score` VALUES (17, 37, 19, 1);
INSERT INTO `subject_score` VALUES (18, 38, 19, 2);
INSERT INTO `subject_score` VALUES (19, 39, 20, 1);
INSERT INTO `subject_score` VALUES (20, 40, 20, 2);
INSERT INTO `subject_score` VALUES (21, 1, 1, 1);
INSERT INTO `subject_score` VALUES (22, 2, 1, 2);
INSERT INTO `subject_score` VALUES (23, 3, 2, 1);
INSERT INTO `subject_score` VALUES (24, 4, 2, 2);
INSERT INTO `subject_score` VALUES (25, 5, 3, 1);
INSERT INTO `subject_score` VALUES (26, 6, 3, 2);
INSERT INTO `subject_score` VALUES (27, 7, 4, 1);
INSERT INTO `subject_score` VALUES (28, 8, 4, 2);
INSERT INTO `subject_score` VALUES (29, 9, 5, 1);
INSERT INTO `subject_score` VALUES (30, 10, 5, 2);
INSERT INTO `subject_score` VALUES (31, 11, 6, 1);
INSERT INTO `subject_score` VALUES (32, 12, 6, 2);
INSERT INTO `subject_score` VALUES (33, 13, 7, 1);
INSERT INTO `subject_score` VALUES (34, 14, 7, 2);
INSERT INTO `subject_score` VALUES (35, 15, 8, 1);
INSERT INTO `subject_score` VALUES (36, 16, 8, 2);
INSERT INTO `subject_score` VALUES (37, 17, 9, 1);
INSERT INTO `subject_score` VALUES (38, 18, 9, 2);
INSERT INTO `subject_score` VALUES (39, 19, 10, 1);
INSERT INTO `subject_score` VALUES (40, 20, 10, 2);
COMMIT;
第一种:
select result1.* from
(select student_id, avg(score) as avgScore from subject_score group by student_id order by avgScore desc) result1
where
(select count(*) from (select student_id, avg(score) as avgScore from subject_score group by student_id) result2) * 0.2
>
(select count(*) from (select student_id, avg(score) as avgScore from subject_score group by student_id) result3 where result1.avgScore < result3.avgScore);
第二种:
select view2.* from (
select view1.*, (@i:=@i+1) as rownum from (
select student_id, avg(score) as avgScore from subject_score
group by student_id order by avgScore desc
) as view1,
(select @i:=0) as init
) as view2
where view2.rownum <= (@i * 0.2);
第二种:需要先排序,再加一次嵌套查询加上行号,因为select中的语句先于order by执行,如果不二次查询,打上的行号可能是错误的(先加行号再排序)。