如何取分组的前3条数据

怎么取分组的前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是什么意思

如何取分组的前3条数据

相关文章:

你感兴趣的文章:

标签云: