7.4. Combining Queries

7.4. Combining Queries
7.4.查询组合
The results of two queries can be combined using the set operations union, intersection, and difference.  The syntax is
两个查询的结果可以使用集合操作符union、intersect和except来组合。语法如下:
 
query1 UNION [ALL] query2
query1 INTERSECT [ALL] query2
query1 EXCEPT [ALL] query2
 
query1 and query2 are queries that can use any of the features discussed up to this point. Set  operations can also be nested and chained, for example
query1和query2可以使用之前讨论的所有特性。集合操作符也可以嵌套和连接,例如:
 
query1 UNION query2 UNION query3
 
which is executed as:
执行顺序为:
 
(query1 UNION query2) UNION query3
 
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 as DISTINCT , unless UNION ALL is used.
UNION将query2结果集追加到query1后(但是行顺序不做保证)。而且,会像distinct那样排除重复行,除非使用了UNION ALL。
 
INTERSECT returns all rows that are both in the result of query1 and in the result of query2 Duplicate rows are eliminated unless INTERSECT ALL is used.
INTERSECT返回既在query1又在query2的行。如果不指定INTERSECT ALL,则会对行去重。
 
EXCEPT returns all rows that are in the result of query1 but not in the result of query2 . (This is  sometimes called the difference between two queries.) Again, duplicates are eliminated unless EXCEPT  ALL is used.
EXCEPT返回在query1中却不在query2中的所有行。(也就是两个查询的差异行)。如果不指定EXCEPT ALL,则会对行去重。
 
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, as described in Section 10.5.
要想使用union、intersect、except,那么两个查询的返回结果必须具有相同的列数量,且相对应的列具有可兼容的数据类型,如 10.5节所述。
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104550465