union 和 union all的區別
相同點和不同點
相同點:
union和union all 都是對於多個查詢結果的並集進行操作
不同點:
1.union 不會輸出兩個結果並集的重複行
2.union all 會輸出兩個結果並集的重複行
實驗表
字段解釋:
xh:學號
xh:姓名
nl:年齡
create table student(xh number,xm varchar2(4),nl int);
insert into student values(1,'A',21);
insert into student values(2,'B',21);
insert into student values(3,'A',21);
insert into student values(4,'A',21);
insert into student values(5,'A',21);
insert into student values(6,'C',21);
insert into student values(7,'B',21);
查看錶
SQL> select * from student;
XH XM NL
---------- ------------ ----------
1 A 21
2 B 21
3 A 21
4 A 21
5 A 21
6 C 21
7 B 21
7 rows selected.
SQL>
例子
union
SQL> select * from student
2 union
3 select * from student where xm='A';
XH XM NL
---------- ------------ ----------
1 A 21
2 B 21
3 A 21
4 A 21
5 A 21
6 C 21
7 B 21
7 rows selected.
SQL>
union all
SQL> select * from student
2 union all
3 select * from student where xm='A';
XH XM NL
---------- ------------ ----------
1 A 21
2 B 21
3 A 21
4 A 21
5 A 21
6 C 21
7 B 21
1 A 21
3 A 21
4 A 21
5 A 21
11 rows selected.
SQL>