Union和Union All的区别
假设我们有一个表Student,包括以下字段与数据:
drop table student;create table student(id int primary key,name nvarchar2(50) not null,score number not null);insert into student values(1,'Aaron',78);insert into student values(2,'Bill',76);insert into student values(3,'Cindy',89);insert into student values(4,'Damon',90);insert into student values(5,'Ella',73);insert into student values(6,'Frado',61);insert into student values(7,'Gill',99);insert into student values(8,'Hellen',56);insert into student values(9,'Ivan',93);insert into student values(10,'Jay',90);commit;
view plaincopyprint?SQL> select * 2 from student 3 where id<4 4 union 5 select * 6 from student 7 where id>2 and id<6 8 ; ID NAME SCORE ---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73 SQL> SQL> select * 2 from student 3 where id<4 4 union 5 select * 6 from student 7 where id>2 and id<6 8 ; ID NAME SCORE---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73SQL>
view plaincopyprint?SQL> select * 2 from student 3 where id<4 4 union all 5 select * 6 from student 7 where id>2 and id<6 8 ; ID NAME SCORE ---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 3 Cindy 89 4 Damon 90 5 Ella 73 6 rows selected. SQL> select * 2 from student 3 where id<4 4 union all 5 select * 6 from student 7 where id>2 and id<6 8 ; ID NAME SCORE---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 3 Cindy 89 4 Damon 90 5 Ella 736 rows selected.
view plaincopyprint?SQL> select * 2 from student 3 where id>2 and id<6 4 union 5 select * 6 from student 7 where id<4 8 ; ID NAME SCORE ---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73 SQL> select * 2 from student 3 where id>2 and id<6 4 union all 5 select * 6 from student 7 where id<4 8 ; ID NAME SCORE ---------- ------------------------------ ---------- 3 Cindy 89 4 Damon 90 5 Ella 73 1 Aaron 78 2 Bill 76 3 Cindy 89 6 rows selected. SQL> select * 2 from student 3 where id>2 and id<6 4 union 5 select * 6 from student 7 where id<4 8 ; ID NAME SCORE---------- ------------------------------ ---------- 1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73SQL> select * 2 from student 3 where id>2 and id<6 4 union all 5 select * 6 from student 7 where id<4 8 ; ID NAME SCORE---------- ------------------------------ ---------- 3 Cindy 89 4 Damon 90 5 Ella 73 1 Aaron 78 2 Bill 76 3 Cindy 896 rows selected.
view plaincopyprint?SQL> select score,id,name 2 from student 3 where id<4 4 union 5 select score,id,name 6 from student 7 where id>2 and id<6 8 ; SCORE ID NAME ---------- ---------- ------------------------------ 73 5 Ella 76 2 Bill 78 1 Aaron 89 3 Cindy 90 4 Damon SQL> select score,id,name 2 from student 3 where id<4 4 union 5 select score,id,name 6 from student 7 where id>2 and id<6 8 ; SCORE ID NAME---------- ---------- ------------------------------ 73 5 Ella 76 2 Bill 78 1 Aaron 89 3 Cindy 90 4 Damon
view plaincopyprint?select score,id,name from student where id > 2 and id < 7 union select score,id,name from student where id < 4 union select score,id,name from student where id > 8 order by id desc select score,id,namefrom studentwhere id > 2 and id < 7unionselect score,id,namefrom studentwhere id < 4unionselect score,id,namefrom studentwhere id > 8order by id desc