前言:在实际项目中对多表进行集合运算使用非常广泛,以下是对集合操作的部分总结。
一,建表,插入测试数据
1 create table a( 2 numbers integer 3 ) 4 5 create table b( 6 numbers integer 7 ) 8 9 insert into a values(7956); 10 insert into a values(7953); 11 insert into a values(7966); 12 insert into a values(7933); 13 commit 14 15 insert into b values(7979); 16 insert into b values(7955); 17 insert into b values(7968); 18 insert into b values(7933); 19 commit
二,求并集,返回a,b两表中所有记录,[去重]
1 select numbers from a 2 union 3 select numbers from b
三,求并集,返回a,b两表中所有记录,[不去重]
1 select numbers from a 2 union all 3 select numbers from a
四,求差集,返回只存在a表中的记录,剔除a表与b表相同的记录
1 --方法一 2 select numbers from a where numbers not in (select numbers from b ) 3 --方法二 4 select numbers from a 5 minus 6 select numbers from b
五,求交集,返回a表与b表中相同的记录
1 --方法一 2 select numbers from a where numbers in (select numbers from b ) 3 --方法二 4 select numbers from a 5 intersect 6 select numbers from b