UNION / UNION ALL 区别

Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;

Union All:对两个结果集进行并集操作,包括重复行,不进行排序;

使用union all:

select top 5 OrderID,[EmployeeID] from dbo.Orders where EmployeeID=1
union all
select top 5 OrderID,[ProductID] from [Order Details] where ProductID=1

  

 

 

使用union:

select top 5 OrderID,[EmployeeID] from dbo.Orders where EmployeeID=1
union
select top 5 OrderID,[ProductID] from [Order Details] where ProductID=1

  

 

附下微软示例数据库连接:NORTHWIND下载

 

posted @ 2016-11-12 23:37  thought  阅读(146)  评论(0编辑  收藏  举报