pg学习_集合操作

集合操作
两个结构相同,内容不完全相同的表
highgo=# select * from test1;
 id |  name  
----+--------
  1 | adam
  2 | lilith
(2 rows)

highgo=# select * from test2;
 id | name 
----+------
  1 | adam
  1 | adam
(2 rows)

1、 union 并集 
union 合并集合,去掉重复行   
union all 合并集合,不去掉重复行 
highgo=# select * from test1
highgo-# union
highgo-# select * from test2;
 id |  name  
----+--------
  1 | adam
  2 | lilith
(2 rows)

highgo=# select * from test1
highgo-# union all
highgo-# select * from test2;
 id |  name  
----+--------
  1 | adam
  2 | lilith
  1 | adam
  1 | adam
(4 rows)

2、intersect 交集 
highgo=# select * from test1
highgo-# intersect
highgo-# select * from test2;
 id | name 
----+------
  1 | adam
(1 row)

3、except 差集 
highgo=# select * from test1
highgo-# except
highgo-# select * from test2;
 id |  name  
----+--------
  2 | lilith
(1 row)

你可能感兴趣的:(postgresql)