首页 > 编程语言 > 详细

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

时间:2019-08-08 16:07:31      阅读:308      评论:0      收藏:0      [点我收藏+]

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

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

原文:https://www.cnblogs.com/SpiritWalker/p/11321304.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!