+ -
当前位置:首页 → 问答吧 → 怎么取分组的前3条数据?

怎么取分组的前3条数据?

时间:2011-12-01

来源:互联网

请问表a
ID TypeID
1 1  
2 1  
3 1
4 2
5 2
6 2
7 2
查出的结果为:
ID TypeID  
1 1
2 1  
4 2
5 2

同一类型取2条数据,按照ID排序,谢谢!

作者: wenle1229   发布时间: 2011-12-01





SELECT * from tt5 a where 2>(select count(*) from tt5 where a.TypeID=TypeID and a.id>id)

作者: wwwwb   发布时间: 2011-12-01

参考下贴中的多种方法

http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....

作者: ACMAIN_CHM   发布时间: 2011-12-01

引用 1 楼 wwwwb 的回复:
SELECT * from tt5 a where 2>(select count(*) from tt5 where a.TypeID=TypeID and a.id>id)

能得到结果,但是还是不明白
请问where 2是什么意思

作者: wenle1229   发布时间: 2011-12-01

select a.TypeID,count(*) from tt5 a left join tt5 b om a.TypeID=b.TypeID and a.id>b.id
group by a.TypeID
看看结果

作者: wwwwb   发布时间: 2011-12-01

相关阅读 更多