topK 算法

有一张表t_stu;其中三个字段:name,gender,grade;要求用一条sql语句查出男生前5名和女生前五名。 

create table t_stu
(
   id int(11),
   name varchar(20),
   gender int(2),
   grade int(12)
)

insert into t_stu values
(1,'Alex',1,91),
(2,'Elena',0,92),
(3,'Alex2',1,81),    
(4,'Elena2',0,82),   
(5,'Alex3',1,71),    
(6,'Elena3',0,72),   
(7,'Alex4',1,61),     
(8,'Elena4',0,62),    
(9,'Alex5',1,51),     
(10,'Elena5',0,52),    
(11,'Alex6',1,101),     
(12,'Elena6',0,1022); 

(
SELECT * 
FROM t_stu 
where gender=0
order by grade  desc
limit 5
)
union
(
 select *
 from t_stu
 where gender=1
 order by grade desc
 limit 5
)
 


topK 算法

 

你可能感兴趣的:(top)