欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

多个结果集union后保持各自原有排序

程序员文章站 2022-05-29 10:09:13
SELECT *FROM ( SELECT TOP (@count1) a.* FROM Article AS a WITH (NOLOCK)LEFT JOIN Article_Type AS at WITH (NOLOCK)ON a.ArticleType=at.ArticleTypeIdWHER ......

select *
from (
select top (@count1) a.* from article as a with (nolock)
left join article_type as at with (nolock)
on a.articletype=at.articletypeid
where a.isdelete=0 and a.status=0
and at.articletypeid=@articletype1---条件变换
and a.issuetime<getdate()
order by a.issuetime desc,a.articletype asc
) t
union all
select *
from (
select top (@count2) a.* from article as a with (nolock)
left join article_type as at with (nolock)
on a.articletype=at.articletypeid
where a.isdelete=0 and a.status=0
and at.articletypeid=@articletype2---条件变换
and a.issuetime<getdate()
order by a.issuetime desc,a.articletype asc
) t
union all
select *
from (
select top (@count3) a.* from article as a with (nolock)
left join article_type as at with (nolock)
on a.articletype=at.articletypeid
where a.isdelete=0 and a.status=0
and at.articletypeid=@articletype3 ---条件变换
and a.issuetime<getdate()
order by a.issuetime desc,a.articletype asc
) t