首页 > 代码库 > UNION / UNION ALL 区别

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下载

 

UNION / UNION ALL 区别