バージョンごとのドキュメント一覧

9.24. 行と配列の比較 #

<title>Row and Array Comparisons</title>

This section describes several specialized constructs for making multiple comparisons between groups of values. These forms are syntactically related to the subquery forms of the previous section, but do not involve subqueries. The forms involving array subexpressions are <productname>PostgreSQL</productname> extensions; the rest are <acronym>SQL</acronym>-compliant. All of the expression forms documented in this section return Boolean (true/false) results. 本節では、値のグループ間で複数の比較を行う、さまざまな特殊化したコンストラクトについて説明します。 この形式は構文的には、前節の副問い合わせ形式と関係しています。しかし、副問い合わせを含みません。 配列副式を含む形式はPostgreSQLの拡張ですが、それ以外はSQL準拠です。 本節で記載した全ての式形式は結果として論理値(真/偽)を返します。

9.24.1. IN #

expression IN (value [, ...])

The right-hand side is a parenthesized list of expressions. The result is <quote>true</quote> if the left-hand expression's result is equal to any of the right-hand expressions. This is a shorthand notation for 右辺は括弧で括られた式のリストです。 左辺の式の結果が右辺の式のいずれかと等しい場合、結果はtrue(真)になります。 これは以下の省略形です。

expression = value1
OR
expression = value2
OR
...

Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand expression yields null, the result of the <token>IN</token> construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values. 左辺の式がNULLを生じる場合、または右側の値に等しいものがなくて少なくとも1つの右辺の行がNULLを持つ場合、IN構文の結果は偽ではなくNULLとなることに注意してください。 これは、NULL値の論理的な組み合わせに対するSQLの標準規則に従うものです。

9.24.2. NOT IN #

expression NOT IN (value [, ...])

The right-hand side is a parenthesized list of expressions. The result is <quote>true</quote> if the left-hand expression's result is unequal to all of the right-hand expressions. This is a shorthand notation for 右辺は括弧で括られた式のリストです。 左辺の式の結果が右辺の式の全てと等しくない場合、結果はです。 これは以下の省略形です。

expression <> value1
AND
expression <> value2
AND
...

Note that if the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand expression yields null, the result of the <token>NOT IN</token> construct will be null, not true as one might naively expect. This is in accordance with SQL's normal rules for Boolean combinations of null values. 左辺の式でNULLが生じる場合、または右辺の値に左辺の式と等しいものがなく、かつ少なくとも1つの右辺の式がNULLを生じる場合、NOT IN構文の結果は、一部の人が予想する真ではなく、NULLとなることに注意してください。 これは、NULL値の論理的な組み合わせに対するSQLの標準規則に従うものです。

ヒント

<literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all cases. However, null values are much more likely to trip up the novice when working with <token>NOT IN</token> than when working with <token>IN</token>. It is best to express your condition positively if possible. 全ての場合において、x NOT IN yNOT (x IN y)と等価です。 しかし、INを使用するよりもNOT INを使用する方が初心者がNULL値による間違いをしやすくなります。 可能な限り条件を肯定的に表現することが最善です。

9.24.3. ANY/SOME (配列) #

<title><literal>ANY</literal>/<literal>SOME</literal> (array)</title>
expression operator ANY (array expression)
expression operator SOME (array expression)

The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given <replaceable>operator</replaceable>, which must yield a Boolean result. The result of <token>ANY</token> is <quote>true</quote> if any true result is obtained. The result is <quote>false</quote> if no true result is found (including the case where the array has zero elements). 右辺は括弧で括られた式で、配列値を返さなければなりません。 左辺の式は配列要素それぞれに対して、指定されたoperatorを使用して評価、比較されます。なお、operatorは結果として論理値を生成する必要があります。 真の結果が1つでもあると、ANYの結果はtrue(真)です。 真の結果がない(配列の要素数がゼロである場合を含む)と、結果はfalse(偽)です。

If the array expression yields a null array, the result of <token>ANY</token> will be null. If the left-hand expression yields null, the result of <token>ANY</token> is ordinarily null (though a non-strict comparison operator could possibly yield a different result). Also, if the right-hand array contains any null elements and no true comparison result is obtained, the result of <token>ANY</token> will be null, not false (again, assuming a strict comparison operator). This is in accordance with SQL's normal rules for Boolean combinations of null values. 配列式がNULL配列を生成する場合、ANYの結果はNULLになります。 左辺式がNULLとなる場合、ANYの結果は通常NULLになります(STRICTでない比較演算子では異なる結果になるかもしれません)。 また、右辺の配列にNULL要素が含まれ、かつ、比較の結果、真が得られなかった場合、ANYの結果は偽ではなくNULLになります(ここでも、STRICTな演算子の場合です)。 これは、NULLに対する、SQLの論理値組み合わせに関する標準規則に従うものです。

<token>SOME</token> is a synonym for <token>ANY</token>. SOMEANYの同義語です。

9.24.4. ALL (配列) #

<title><literal>ALL</literal> (array)</title>
expression operator ALL (array expression)

The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given <replaceable>operator</replaceable>, which must yield a Boolean result. The result of <token>ALL</token> is <quote>true</quote> if all comparisons yield true (including the case where the array has zero elements). The result is <quote>false</quote> if any false result is found. 右辺は括弧で括られた式で、配列値を返さなければなりません。 左辺の式は配列の要素それぞれに対して、指定されたoperatorを使用して評価、比較されます。なお、operatorは結果として論理値を生成する必要があります。 全ての比較が真になる場合(配列の要素数がゼロである場合を含む)、ALLの結果はtrue(真)です。 1つでも偽の結果があると、結果はfalse(偽)です。

If the array expression yields a null array, the result of <token>ALL</token> will be null. If the left-hand expression yields null, the result of <token>ALL</token> is ordinarily null (though a non-strict comparison operator could possibly yield a different result). Also, if the right-hand array contains any null elements and no false comparison result is obtained, the result of <token>ALL</token> will be null, not true (again, assuming a strict comparison operator). This is in accordance with SQL's normal rules for Boolean combinations of null values. 配列式がNULL配列を生成する場合、ALLの結果はNULLになります。 左辺式がNULLとなる場合、ALLの結果は通常NULLになります(厳格でない比較演算子では異なる結果になるかもしれません)。 また、右辺の配列にNULL要素が含まれ、かつ、比較の結果、偽が得られなかった場合、ALLの結果は真ではなくNULLになります(ここでも、厳格な演算子の場合です)。 これは、NULLに対する、SQLの論理値組み合わせに関する標準規則に従うものです。

9.24.5. 行コンストラクタの比較 #

<title>Row Constructor Comparison</title>
row_constructor operator row_constructor

Each side is a row constructor, as described in <xref linkend="sql-syntax-row-constructors"/>. The two row constructors must have the same number of fields. The given <replaceable>operator</replaceable> is applied to each pair of corresponding fields. (Since the fields could be of different types, this means that a different specific operator could be selected for each pair.) All the selected operators must be members of some B-tree operator class, or be the negator of an <literal>=</literal> member of a B-tree operator class, meaning that row constructor comparison is only possible when the <replaceable>operator</replaceable> is <literal>=</literal>, <literal>&lt;&gt;</literal>, <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>&gt;</literal>, or <literal>&gt;=</literal>, or has semantics similar to one of these. 4.2.13で説明されているように、両辺は行コンストラクタです。 2つの行コンストラクタは同じ数のフィールドを持つ必要があります。 指定された演算子は、対応するフィールドの各ペアに適用されます。 (フィールドのタイプが異なる場合があるため、これは、各ペアに対して異なる特定の演算子を選択できることを意味します。) 選択される演算子はすべて、B-ツリー演算子クラスのメンバ、またはB-ツリー演算子クラスの=メンバの否定子である必要があります。 つまり、行コンストラクタ比較は、演算子=<><<=>>=の場合、またはこれらのいずれかに類似したセマンティクスを持つ場合にのみ可能です。

The <literal>=</literal> and <literal>&lt;&gt;</literal> cases work slightly differently from the others. Two rows are considered equal if all their corresponding members are non-null and equal; the rows are unequal if any corresponding members are non-null and unequal; otherwise the result of the row comparison is unknown (null). =<>の場合、他と動作が多少異なります。 2つの行は対応する全ての構成要素が非NULLかつ等しい場合に等しいとみなされます。 1つでも構成要素が非NULLかつ等しくない場合、2つの行は等しくないとみなされます。 それ以外の場合、その行の比較結果は不明(NULL)です。

For the <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>&gt;</literal> and <literal>&gt;=</literal> cases, the row elements are compared left-to-right, stopping as soon as an unequal or null pair of elements is found. If either of this pair of elements is null, the result of the row comparison is unknown (null); otherwise comparison of this pair of elements determines the result. For example, <literal>ROW(1,2,NULL) &lt; ROW(1,3,0)</literal> yields true, not null, because the third pair of elements are not considered. <<=>>=の場合、行の要素は左から右に比較されます。そして、不等またはNULLの組み合わせが見つかったところで停止します。 要素の組み合わせのどちらかがNULLであった場合、行比較の結果は不明(NULL)です。さもなくば、要素の組み合わせの比較により結果が決まります。 例えば、ROW(1,2,NULL) < ROW(1,3,0)は、3番目の要素の組み合わせまで進まないため、NULLではなく真を返します。

row_constructor IS DISTINCT FROM row_constructor

This construct is similar to a <literal>&lt;&gt;</literal> row comparison, but it does not yield null for null inputs. Instead, any null value is considered unequal to (distinct from) any non-null value, and any two nulls are considered equal (not distinct). Thus the result will either be true or false, never null. このコンストラクトは<>行比較と類似していますが、NULL入力に対してNULLを生成しない点が異なります。 その代わりに、全てのNULL値は非NULL値と等しくない(DISTINCT FROM)ものとみなされ、また、2つのNULLは等しい(NOT DISTINCT)ものとみなされます。 したがって、結果は真か偽のいずれかで、NULLにはなりません。

row_constructor IS NOT DISTINCT FROM row_constructor

This construct is similar to a <literal>=</literal> row comparison, but it does not yield null for null inputs. Instead, any null value is considered unequal to (distinct from) any non-null value, and any two nulls are considered equal (not distinct). Thus the result will always be either true or false, never null. このコンストラクトは=行比較と類似していますが、NULL入力に対してNULLを生成しません。 代わりに、NULL値を、すべての非NULLの値に対して不等(DISTINCT FROM)とみなし、2つのNULLを等しいもの(NOT DISTINCT)とみなします。 したがって、結果は常に真か偽となり、NULLになることはありません

9.24.6. 複合型の比較 #

<title>Composite Type Comparison</title>
record operator record

The SQL specification requires row-wise comparison to return NULL if the result depends on comparing two NULL values or a NULL and a non-NULL. <productname>PostgreSQL</productname> does this only when comparing the results of two row constructors (as in <xref linkend="row-wise-comparison"/>) or comparing a row constructor to the output of a subquery (as in <xref linkend="functions-subquery"/>). In other contexts where two composite-type values are compared, two NULL field values are considered equal, and a NULL is considered larger than a non-NULL. This is necessary in order to have consistent sorting and indexing behavior for composite types. SQL仕様では、結果が2つのNULL値、またはNULLと非NULLの比較に依存するのであれば、行の観点からの比較はNULLを返すことを要求されています。 PostgreSQLは、(9.24.5にあるように)2つの行コンストラクタの出力の比較を行う時、または副問い合わせの出力に対し(9.23にあるように)行コンストラクタの比較を行う時のみこれを実施します。 2つの複合型の値が比較されるほかの状況では、2つのNULLフィールドの値は等しいと考えられ、NULLは非NULLより大きいとみなされます。 複合型に対して、これは一貫した並べ替えとインデックス付け動作担保のため必要です。

Each side is evaluated and they are compared row-wise. Composite type comparisons are allowed when the <replaceable>operator</replaceable> is <literal>=</literal>, <literal>&lt;&gt;</literal>, <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>&gt;</literal> or <literal>&gt;=</literal>, or has semantics similar to one of these. (To be specific, an operator can be a row comparison operator if it is a member of a B-tree operator class, or is the negator of the <literal>=</literal> member of a B-tree operator class.) The default behavior of the above operators is the same as for <literal>IS [ NOT ] DISTINCT FROM</literal> for row constructors (see <xref linkend="row-wise-comparison"/>). 各辺が評価され、行単位で比較が行なわれます。 複合型の比較はoperator=<><<=>>=またはそのいずれかと類似の意味を持つ場合に許されます。 (正確には、演算子はB-tree演算子クラスのメンバである場合、またはB-tree演算子クラスの=メンバの否定子である場合に行比較演算子となり得ます。) 上記の演算子のデフォルトの動作は、行コンストラクタに対するIS [ NOT ] DISTINCT FROMと同じです(9.24.5参照)。

To support matching of rows which include elements without a default B-tree operator class, the following operators are defined for composite type comparison: <literal>*=</literal>, <literal>*&lt;&gt;</literal>, <literal>*&lt;</literal>, <literal>*&lt;=</literal>, <literal>*&gt;</literal>, and <literal>*&gt;=</literal>. These operators compare the internal binary representation of the two rows. Two rows might have a different binary representation even though comparisons of the two rows with the equality operator is true. The ordering of rows under these comparison operators is deterministic but not otherwise meaningful. These operators are used internally for materialized views and might be useful for other specialized purposes such as replication and B-Tree deduplication (see <xref linkend="btree-deduplication"/>). They are not intended to be generally useful for writing queries, though. デフォルトのB-tree演算子クラスを持たない要素を含む行の一致をサポートするために、いくつかの演算子が複合型の比較のために定義されています。 それは*=*<>*<*<=*>*>=です。 上記の演算子は2つの行の内部バイナリ表現を比較します。 2つの行の等価演算子での比較が真であっても、2つの行はバイナリ表現が異なるかもしれません。 上記の比較演算子での行の順序は決定論的ですが、それ以外は意味がありません。 上記の演算子はマテリアライズドビューで内部的に使われ、レプリケーションやB-Treeの重複除去(67.4.3参照)のような他の特定の目的のためには有用かもしれませんが、問い合わせを書くのに一般的に有用であるようには意図していません。