--Oracle中的复合查询 复合查询:包含集合运算(操作)的查询 常见的集合操作有: union: 两个查询的并集(无重复行、按第一个查询的第一列升序排序) union all:两个查询的并集(有重复行) intersect:两个查询的交集(无重复行、按第一个查询的第一列升序排序) minus: 两个查询的差集(无重复行、按第一个查询的第一列升序排序),取第一张表有而第二张表没有的所有记录 由于union、intersect、minus存在排序,故而对sql性能的影响很大,建议少用。 --测试 create table t1 (id number,name varchar2(20)); create table t2 (id number,name varchar2(20)); insert into t1 values(1,'表1'); insert into t1 values(2,'表1'); insert into t1 values(3,'表1'); insert into t1 values(4,'表1'); insert into t1 values(5,'表1'); insert into t2 values(3,'表2'); insert into t2 values(4,'表2'); insert into t2 values(5,'表2'); insert into t2 values(6,'表2'); insert into t2 values(7,'表2'); --查询t1和t2 SQL> select * from t1; ID NAME ---------- -------------------- 1 表1 2 表1 3 表1 4 表1 5 表1 SQL> select * from t2; ID NAME ---------- -------------------- 3 表2 4 表2 5 表2 6 表2 7 表2 --并集 --1.union select id from t1 union select id from t2 ; SQL> select id from t1 2 union 3 select id from t2 ; ID ---------- 1 2 3 4 5 6 7 7 rows selected --2.union all select id from t1 union all select id from t2; SQL> select id from t1 2 union all 3 select id from t2; ID ---------- 1 2 3 4 5 3 4 5 6 7 10 rows selected --交集 select id from t1 intersect select id from t2; SQL> select id from t1 2 intersect 3 select id from t2; ID ---------- 3 4 5 --差集 select id from t1 minus select id from t2; SQL> select id from t1 2 minus 3 select id from t2; ID ---------- 1 2