create table #article(id int ,modelId int ,modelNo varchar(50), title varchar(200) )
declare @modelId int DECLARE Cur1 cursor for
SELECT id, modelNo FROM pageModel
OPEN Cur1
FETCH next from Cur1 INTO @modelId WHILE @@fetch_status<>-1
BEGIN
Insert Into #article
Select a.id,a.modelId ,b.modelNo,a.title
From article a,pageModel b
Where a.modelId=b.id and
a.id in(select top 5 id from article where modelId= @modelId )
FETCH next from Cur1 INTO @modelId
END
CLOSE Cur1
DEALLOCATE Cur1Select * from #article
Order by modelId,id
Drop table #article
declare @modelId int DECLARE Cur1 cursor for
SELECT id, modelNo FROM pageModel
OPEN Cur1
FETCH next from Cur1 INTO @modelId WHILE @@fetch_status<>-1
BEGIN
Insert Into #article
Select a.id,a.modelId ,b.modelNo,a.title
From article a,pageModel b
Where a.modelId=b.id and
a.id in(select top 5 id from article where modelId= @modelId )
FETCH next from Cur1 INTO @modelId
END
CLOSE Cur1
DEALLOCATE Cur1Select * from #article
Order by modelId,id
Drop table #article
--将数据放入一个临时表.
elect a.modelNo , b.id, b.title into temp from article a, pageModel b where a.modelId = b.id
--获取每类的前五条记录.
select * from temp as t
where (select count(*) from temp where modelNo = t.modelNo and id < t.id) < 5
文章表 article 字段 id, modelId(对应 pageModel 中的 id), title
版块表 pageModel 字段 id, modelNo--OK ,
select * from article a where a.id in (select top 5 id from article where pageModel = a.pageModel )