日期:2014-05-16  浏览次数:20850 次

怎么取分组的前3条数据?
请问表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排序,谢谢!

------解决方案--------------------




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

------解决方案--------------------
参考下贴中的多种方法

http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....
------解决方案--------------------
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
看看结果

------解决方案--------------------
探讨
引用:
SELECT * from tt5 a where 2>(select count(*) from tt5 where a.TypeID=TypeID and a.id>id)

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