天天看点

mysql单个表找出最好或者最多的一条和二条数据

在统计数据的时候,会遇到这种场景,找出这个表中按照某个字段的排序,最好的一条或者最好的两条,遇到这种场景应该怎么样处理?

比如: temp_athlete_score的表数据结构为

mysql单个表找出最好或者最多的一条和二条数据
select * from temp_athlete_score as m where (
select count(*) from temp_athlete_score as f
where f.f_card_num=m.f_card_num and f.f_rank_score<=m.f_rank_score
)<=1 and f_card_num='XXX'       
select * from temp_athlete_score as m where (
select count(*) from temp_athlete_score as f
where f.f_card_num=m.f_card_num and f.f_rank_score<=m.f_rank_score
)<=2 and f_card_num='XXX'       

继续阅读