PostgreSQL UNION[ALL],INTERSECT [ALL],EXCEPT [ALL]

表结构:

postgres=# \d person 
         Table "public.person"
 Column |       Type        | Modifiers 
--------+-------------------+-----------
 id     | integer           | 
 name   | character varying | 
数据:
postgres=# select * from person;
 id | name 
----+------
  1 | aa
  2 | bb
  3 | cc
  4 | dd
  5 | ee
  6 | aa
  7 | bb
  8 | aa
(8 rows)


1.UNION[ALL] 合并两个结果集,使用ALL不去除两个结果集中重复的。

UNION effectively appends the result of query2 to the result of query1 (although there is no guarantee that this is the order in which the rows are actually returned). Furthermore,it eliminates duplicate rows from its result,in the same way asdisTINCT,unless UNION ALL is used.

postgres=# select name from person where id<5 union all select name from person where id>5;
 name 
------
 aa
 bb
 cc
 dd
 aa
 bb
(6 rows)

postgres=# select name from person where id<5 union select name from person where id>5;
 name 
------
 bb
 cc
 dd
 aa
(4 rows)


2.INTERSECT [ALL] 查询两个结果集的交集。

INTERSECT returns all rows that are both in the result ofquery1 and in the result ofquery2. Duplicate rows are eliminated unlessINTERSECT ALL is used.

postgres=# select name from person where id<5 intersect select name from person where id>5;
 name 
------
 bb
 aa
(2 rows)

3.EXCEPT [ALL] 查询在前一个结果集中但是不再后面一个结果集中的记录。

EXCEPT returns all rows that are in the result ofquery1 but not in the result ofquery2. (This is sometimes called thedifference between two queries.) Again,duplicates are eliminated unlessEXCEPT ALL is used.

postgres=# select name from person where id<5 except select name from person where id>5;
 name 
------
 cc
 dd
(2 rows)
4. note:
In order to calculate the union,intersection,or difference of two queries,the two queries must be"union compatible",which means that they return the same number of columns and the corresponding columns have compatible data types。

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...