create table my_brick_collection (
colour varchar2(10),
shape varchar2(10),
weight integer
);
create table your_brick_collection (
height integer,
width integer,
depth integer,
colour varchar2(10),
shape varchar2(10)
);
insert into my_brick_collection values ( 'red', 'cube', 10 );
insert into my_brick_collection values ( 'blue', 'cuboid', 8 );
insert into my_brick_collection values ( 'green', 'pyramid', 20 );
insert into my_brick_collection values ( 'green', 'pyramid', 20 );
insert into my_brick_collection values ( null, 'cuboid', 20 );
insert into your_brick_collection values ( 2, 2, 2, 'red', 'cube' );
insert into your_brick_collection values ( 2, 2, 2, 'blue', 'cube' );
insert into your_brick_collection values ( 2, 2, 8, null, 'cuboid' );
commit;
select * from my_brick_collection;
select * from your_brick_collection;
Union: 运算符将两个或多个表合并为一个结果集。
select * 结合会导致错误
select * from my_brick_collection
union
select * from your_brick_collection;
请选择公共列。这就是颜色和形状。因此,此查询返回表中所有 ( colour, shape ) 值的列表
select colour, shape from my_brick_collection
union
select colour, shape from your_brick_collection;
把两个表列加一块,不包括重复的
使用 distinct这将消除重复项。
select distinct * from my_brick_collection;
select distinct shape from your_brick_collection;
显示所有
select colour, shape from my_brick_collection
union all
select colour, shape from your_brick_collection;
select distinct * from (
select colour, shape from my_brick_collection
union all
select colour, shape from your_brick_collection
);
Minus:差集的运算符:减去
select colour, shape from your_brick_collection
minus
select colour, shape from my_brick_collection;
select colour, shape from my_brick_collection
minus
select colour, shape from your_brick_collection
看晕了
select colour, shape from my_brick_collection mbc
where not exists (
select null from your_brick_collection ybc
where ( ybc.colour = mbc.colour or ( ybc.colour is null and mbc.colour is null ) )
and ybc.shape = mbc.shape
);
找到存在于两个表中的共有值
select colour, shape from your_brick_collection ybc
where exists (
select null from my_brick_collection mbc
where ( ybc.colour = mbc.colour or ( ybc.colour is null and mbc.colour is null ) )
and ybc.shape = mbc.shape
);
intersect 相交
还有一个运算符来查找公共值:
select colour, shape from your_brick_collection
intersect
select colour, shape from my_brick_collection;
查找两个表之间的差异(对称差异)
select colour, shape from your_brick_collection
minus
select colour, shape from my_brick_collection
union all
select colour, shape from my_brick_collection
minus
select colour, shape from your_brick_collection;
blue cuboid and green pyramid
select * from (
select colour, shape from your_brick_collection
minus
select colour, shape from my_brick_collection
) union all (
select colour, shape from my_brick_collection
minus
select colour, shape from your_brick_collection
);
select * from (
select colour, shape from your_brick_collection
union all
select colour, shape from my_brick_collection
) minus (
select colour, shape from my_brick_collection
intersect
select colour, shape from your_brick_collection