分组取前N记录

查出每关排名前10的
SELECT 
  p.level,
  p.user_id,
  u.name,
  u.icon_Id,
  p.top_Bo,
  p.top_Kill 
FROM
  `sg_level_top_lastweek` p
  JOIN `sg_user` u ON p.user_id = u.id AND u.is_blacklist = 0 
WHERE 10 >  (SELECT COUNT(*) FROM sg_level_top_lastweek a WHERE a.level = p.level AND (a.top_Bo > p.top_Bo OR (a.top_Bo=p.top_Bo AND a.top_Kill>p.top_Kill) )) 
ORDER BY p.level,p.top_Bo DESC,p.top_Kill DESC 


参考:
一条SQL语句查询出每个班前10名学生数据

select * from 学生信息表 a 

where 10 >  (select count(*) from 学生信息表 where 班级ID = a.班级ID and 班内名次 > a.班内名次)


http://bbs.csdn.net/topics/391822538

你可能感兴趣的:(sql)