SQL Server sql 查询结果合并union all用法

2022-11-12 09:54:01
内容摘要
这篇文章主要为大家详细介绍了SQL Server sql 查询结果合并union all用法,具有一定的参考价值,可以用来参考一下。 对此感兴趣的朋友,看看idc笔记做的技术笔记! 代码如下: --
文章正文

这篇文章主要为大家详细介绍了SQL Server sql 查询结果合并union all用法,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记!

代码如下:


--合并重复行 
select * from A 
union 
select * from B 


--不合并重复行 
select * from A 
union all 
select * from B 


按某个字段排序 
--合并重复行 
select * 
from ( 
select * from A 
union 
select * from B) AS T 
order by 字段名 

--不合并重复行 
select * 
from ( 
select * from A 
union all 
select * from B) AS T 
order by 字段名 

//sql server版 
Select * From ( 
select top 2 id,adddate,title,url from bArticle where ClassId=1 order by adddate desc) A 
Union All 
Select * From ( 
select top 2 id,adddate,title,url from bArticle where ClassId=2 order by adddate desc) B 
Union All 
Select * From ( 
select top 2 id,adddate,title,url from bArticle where ClassId=3 order by adddate desc) C 
Union All 
Select * From ( 
select top 2 id,adddate,title,url from bArticle where ClassId=4 order by adddate desc) D 


//mysql版 
Select * From ( 
select id,adddate,title,url from bArticle where ClassId=1 order by adddate desc limit 0,2) A 
Union All 
Select * From ( 
select id,adddate,title,url from bArticle where ClassId=2 order by adddate desc limit 0,2) B 
Union All 
Select * From ( 
select id,adddate,title,url from bArticle where ClassId=3 order by adddate desc limit 0,2) C 
Union All 
Select * From ( 
select id,adddate,title,url from bArticle where ClassId=4 order by adddate desc limit 0,2) D

注:关于SQL Server sql 查询结果合并union all用法的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!