UNION, INTERSECT, EXCEPT) #The results of two queries can be combined using the set operations union, intersection, and difference. The syntax is 2つの問い合わせの結果は、和、積、差の集合演算を使って結合することができます。 構文は以下の通りです。
query1UNION [ALL]query2query1INTERSECT [ALL]query2query1EXCEPT [ALL]query2
where <replaceable>query1</replaceable> and
<replaceable>query2</replaceable> are queries that can use any of
the features discussed up to this point.
query1とquery2は、これまで説明した任意の機能をすべて使用することができる問い合わせです。
<literal>UNION</literal> effectively appends the result of
<replaceable>query2</replaceable> to the result of
<replaceable>query1</replaceable> (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 <literal>DISTINCT</literal>, unless <literal>UNION ALL</literal> is used.
UNIONは、query2の結果をquery1の結果に付加します(しかし、この順序で実際に行が返される保証はありません)。
さらに、UNION ALLを指定しないと、DISTINCTと同様に、結果から重複している行を削除します。
<literal>INTERSECT</literal> returns all rows that are both in the result
of <replaceable>query1</replaceable> and in the result of
<replaceable>query2</replaceable>. Duplicate rows are eliminated
unless <literal>INTERSECT ALL</literal> is used.
INTERSECTは、query1の結果とquery2の結果の両方に含まれているすべての行を返します。
INTERSECT ALLを使用しないと、重複している行は削除されます。
<literal>EXCEPT</literal> returns all rows that are in the result of
<replaceable>query1</replaceable> but not in the result of
<replaceable>query2</replaceable>. (This is sometimes called the
<firstterm>difference</firstterm> between two queries.) Again, duplicates
are eliminated unless <literal>EXCEPT ALL</literal> is used.
EXCEPTは、query1の結果には含まれているけれども、query2の結果には含まれていないすべての行を返します。
(これが2つの問い合わせの差であると言われることがあります。)
この場合も、EXCEPT ALL を使用しないと、重複している行は削除されます。
In order to calculate the union, intersection, or difference of two queries, the two queries must be <quote>union compatible</quote>, which means that they return the same number of columns and the corresponding columns have compatible data types, as described in <xref linkend="typeconv-union-case"/>. 2つの問い合わせの和、積、差を算出するために、そこの2つの問い合わせは「union互換」でなければいけません。 つまり、その問い合わせが同じ数の列を返し、対応する列は互換性のあるデータ型(10.5を参照)でなければなりません。
Set operations can be combined, for example 集合演算は組み合わせることができます。以下に例を示します。
query1UNIONquery2EXCEPTquery3
which is equivalent to これは以下と同じです。
(query1UNIONquery2) EXCEPTquery3
As shown here, you can use parentheses to control the order of
evaluation. Without parentheses, <literal>UNION</literal>
and <literal>EXCEPT</literal> associate left-to-right,
but <literal>INTERSECT</literal> binds more tightly than those two
operators. Thus
ここに示すように、括弧を使用して評価の順序を制御できます。
括弧がない場合、UNIONとEXCEPTは左から右に関連付けられますが、INTERSECTはこれらの2つの演算子よりも強く結合します。
つまり、
query1UNIONquery2INTERSECTquery3
means は以下を意味します。
query1UNION (query2INTERSECTquery3)
You can also surround an individual <replaceable>query</replaceable>
with parentheses. This is important if
the <replaceable>query</replaceable> needs to use any of the clauses
discussed in following sections, such as <literal>LIMIT</literal>.
Without parentheses, you'll get a syntax error, or else the clause will
be understood as applying to the output of the set operation rather
than one of its inputs. For example,
個々のqueryを括弧で囲むこともできます。
これは、queryがLIMITのような、以下の節で説明されている句のいずれかを使用する必要がある場合に重要です。
括弧がないと、構文エラーが発生します。さもなければ、この句は集合演算の入力の1つではなく、集合演算の出力に適用されると解釈されます。
例えば、以下のようになります。
SELECT a FROM b UNION SELECT x FROM y LIMIT 10
is accepted, but it means は、受け入れられますが、以下を意味します。
(SELECT a FROM b UNION SELECT x FROM y) LIMIT 10
not 以下ではありません。
SELECT a FROM b UNION (SELECT x FROM y LIMIT 10)