oracleunion用法
1、新建一个教师表包括以下字段与数据:
create table teacher
(
id int primary key,
name nvarchar2(50) not null,
score number not null
);
insert into teacher values(1,'Aaron',250);
insert into teacher values(2,'Bill',250);
insert into teacher values(3,'Cindy',250);
insert into teacher values(4,'Damon',260);
insert into teacher values(5,'Ella',260);
insert into teacher values(6,'Frado',260);
insert into teacher values(7,'Gill',260);
insert into teacher values(8,'Hellen',260);
insert into teacher values(9,'Ivan',260);
insert into teacher values(10,'Jay',260);
commit;
2、select *
from teacher
where id < 4
union
select *
from teacher
where id > 2 and id < 6
结果将是
1 Aaron 250
2 Bill 250
3 Cindy 250
4 Damon 260
5 Ella 260
3、union指令的目的是将两个 SQL 语句的结果合并起来,可以查看你要的查询结果。会排除重复的记录 。
4、然而 union all 不会排除重复的记录。
看下面的示例:
5、如果换成Union All连接两个结果集
select *
from teacher
where id < 4
union all
select *
from teacher
where id > 2 and id < 6
则返回结果是:
1 Aaron 250
2 Bill 250
3 Cindy 250
3 Cindy 250
4 Damon 260
5 Ella 260
6、通过对比,希望我们能清楚的认识这个union的用法。
如果对您有帮助,请点击下面的投票支持我,谢谢。