select top 5 * from b where type=1 union select top 5 * from b where type=2 union select top 5 * from b where type=3
select top 5 * from b where type=1 union select top 5 * from b where type=2 union select top 5 * from b where type=3 ========================================= 这样是不行的,类别是个添加删除的! 再求新法
select a.* from news a ,category c where id in( select top 2 b.id from news b where b.categoryid = c.categoryid order by b.id desc )
你可以参考以下这个存储过程:CREATE proc up_GetIndexArticle@chvSpecialId_1 int=null, @chvSpecialId_2 int=null, @chvSpecialId_3 int=null as create table #MyTableVar (ArticleId bigint, title nvarchar(500), channel char(100)) if @chvSpecialId_1 is not null insert into #MyTableVar select top 5 * from V_ArticleSpecial where specialId=@chvSpecialId_1if @chvSpecialId_2 is not null insert into #MyTableVar select top 5 * from V_ArticleSpecial where specialId=@chvSpecialId_2if @chvSpecialId_3 is not null insert into #MyTableVar * from V_ArticleSpecial where specialId=@chvSpecialId_3select * from #MyTableVar order by ArticleId desc drop table #MyTableVarGO
union
select top 5 * from b where type=2
union
select top 5 * from b where type=3
union
select top 5 * from b where type=2
union
select top 5 * from b where type=3
=========================================
这样是不行的,类别是个添加删除的!
再求新法
select top 2 b.id from news b where
b.categoryid = c.categoryid
order by b.id desc
)
@chvSpecialId_2 int=null,
@chvSpecialId_3 int=null
as
create table #MyTableVar
(ArticleId bigint,
title nvarchar(500),
channel char(100))
if @chvSpecialId_1 is not null
insert into #MyTableVar select top 5 *
from V_ArticleSpecial where specialId=@chvSpecialId_1if @chvSpecialId_2 is not null
insert into #MyTableVar select top 5 *
from V_ArticleSpecial where specialId=@chvSpecialId_2if @chvSpecialId_3 is not null
insert into #MyTableVar *
from V_ArticleSpecial where specialId=@chvSpecialId_3select * from #MyTableVar order by ArticleId desc
drop table #MyTableVarGO