MySQL中TOP N记录

MySQL获取分组后的TOP N记录

select * from test1 a where 2 > (select count(*) from test1 where course=a.course and score>a.score)

select advertiser_id,server_update_time from ad_plan a
where 2> (select count(*) from ad_plan where advertiser_id=a.advertiser_id and server_update_time>a.server_update_time)
ORDER by advertiser_id,server_update_time DESC

非分组topN记录或从第几条到第几条

select * from ad_plan limit 1,1 (获取第二条,第一个1为起始offset,第二个1为取1条)
select * from ad_plan limit 0,2 (获取前两条)

你可能感兴趣的:(MySQL中TOP N记录)