"景先生毕设|www.jxszl.com

union 和union all 区别,Intersect、 Minus 函数介绍

2023-09-12 15:40编辑: www.jxszl.com景先生毕设

union 和union all 区别,Intersect、 Minus 函数介绍

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

语句示例:
 create table  test1211
 (
 id  varchar(10),
 name varchar(10),
 score number
 )
 
insert into test1211   values('1','张三',50) ;
insert into test1211   values('2','张三',60) ;
insert into test1211   values('3','张三',70) ;
insert into test1211   values('3','张三',70) ;  -- 重复 数据

insert into test1211   values('4','李四',50) ;
insert into test1211   values('5','李四',50) ;
insert into test1211   values('6','李四',70) ;
 
Intersect 取交集:
select score from test1211 a where  name  ='张三'
Intersect
select score from test1211 a where  name  ='李四'
 输出:
ID NAME  SCORE
1 张三 50
2 张三 60
3 张三 70
4 李四 50
5 李四 50
6 李四 70

我们发现按照 默认的 顺序排,中间重复行没有了。
 
union all合并 :
select * from  test1211 where  name  ='李四'
union all
select * from  test1211 where  name  ='张三'
没有排序,怎么写,就是什么书序,中间重复行还保留

ID NAME SCORE
1 4 李四 50
2 5 李四 50
3 6 李四 70
4 1 张三 50
5 2 张三 60
6 3 张三 70
7 3 张三 70
  ID NAME  SCORE
4 李四 50
5 李四 50
6 李四 70


原文链接:http://www.jxszl.com/biancheng/shujuku/445466.html