表结构:
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)
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)
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。 总结
以上是内存溢出为你收集整理的PostgreSQL UNION[ALL],INTERSECT [ALL],EXCEPT [ALL]全部内容,希望文章能够帮你解决PostgreSQL UNION[ALL],INTERSECT [ALL],EXCEPT [ALL]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)