The usual comparison operators are available, as shown in <xref linkend="functions-comparison-op-table"/>. 表 9.1に示すように、通常の比較演算子が使用可能です。
表9.1 比較演算子
| 演算子 | 説明 |
|---|---|
datatype < datatype
→ boolean
| 小なり |
datatype > datatype
→ boolean
| 大なり |
datatype <= datatype
→ boolean
| 等しいかそれ以下 |
datatype >= datatype
→ boolean
| 等しいかそれ以上 |
datatype = datatype
→ boolean
| 等しい |
datatype <> datatype
→ boolean
| 等しくない |
datatype != datatype
→ boolean
| 等しくない |
<literal><></literal> is the standard SQL notation for <quote>not
equal</quote>. <literal>!=</literal> is an alias, which is converted
to <literal><></literal> at a very early stage of parsing.
Hence, it is not possible to implement <literal>!=</literal>
and <literal><></literal> operators that do different things.
<>が標準SQLにおける「等しくない」の記法です。
!=はその別名で、構文解析のごく初期に<>に変換されます。
ですから!=演算子と<>演算子に異なる処理を行わせる実装はできません。
These comparison operators are available for all built-in data types that have a natural ordering, including numeric, string, and date/time types. In addition, arrays, composite types, and ranges can be compared if their component data types are comparable. これらの比較演算子は、数値、文字列、日付、時刻データ型などの自然な順序付けを持つすべての組み込みデータ型に用意されています。 更に、要素となるデータ型が比較可能なら、配列、複合データ型、範囲は比較可能です。
It is usually possible to compare values of related data
types as well; for example <type>integer</type> <literal>></literal>
<type>bigint</type> will work. Some cases of this sort are implemented
directly by <quote>cross-type</quote> comparison operators, but if no
such operator is available, the parser will coerce the less-general type
to the more-general type and apply the latter's comparison operator.
通常関連性のあるデータ型も比較することができます。
たとえばinteger >bigintも可能です。
ある場合にはこれらの比較は「型をまたがる」比較演算子で直接実装されています。そうした演算子がなければ、パーサはより一般的ではない型をより一般的な型に変換して後者の比較演算子に適用します。
As shown above, all comparison operators are binary operators that
return values of type <type>boolean</type>. Thus, expressions like
<literal>1 < 2 < 3</literal> are not valid (because there is
no <literal><</literal> operator to compare a Boolean value with
<literal>3</literal>). Use the <literal>BETWEEN</literal> predicates
shown below to perform range tests.
上で示したように、全ての比較演算子は二項演算子で、boolean型の値を返します。
ですから1 < 2 < 3のような式は(ブール値と3を比較する<演算子がないので)無効です。
下で示すBETWEEN述語を使って範囲検査を行ってください。
There are also some comparison predicates, as shown in <xref linkend="functions-comparison-pred-table"/>. These behave much like operators, but have special syntax mandated by the SQL standard. 表 9.2に示すように、比較述語がいくつかあります。 これらは演算子と同様に振る舞いますが、標準SQLによって強制される特別の構文があります。
表9.2 比較述語
Predicate 述語 Description 説明 Example(s) 例 |
|---|
Between (inclusive of the range endpoints). 間にある(範囲の端点を含む)。
|
Not between (the negation of <literal>BETWEEN</literal>).
間にない(
|
Between, after sorting the two endpoint values. 2つの端点値をソートした上で、間にある。
|
Not between, after sorting the two endpoint values. 2つの端点値をソートした上で、間にない。
|
Not equal, treating null as a comparable value. NULLを比較可能な値とした上で、等しくない。
|
Equal, treating null as a comparable value. NULLを比較可能な値とした上で、等しい。
|
Test whether value is null. 値がNULLかどうか検査する。
|
Test whether value is not null. 値がNULLではないかどうか検査する。
|
Test whether value is null (nonstandard syntax). 値がNULLかどうか検査する。(非標準構文) |
Test whether value is not null (nonstandard syntax). 値がNULLではないかどうか検査する。(非標準構文) |
Test whether boolean expression yields true. 論理式の結果が真となるかどうか検査する。
|
Test whether boolean expression yields false or unknown. 論理式の結果が偽または不明となるかどうか検査する。
|
Test whether boolean expression yields false. 論理式の結果が偽となるかどうか検査する。
|
Test whether boolean expression yields true or unknown. 論理式の結果が真または不明となるかどうか検査する。
|
Test whether boolean expression yields unknown. 論理式の結果が不明となるかどうか検査する。
|
Test whether boolean expression yields true or false. 論理式の結果が真または偽となるかどうか検査する。
|
The <token>BETWEEN</token> predicate simplifies range tests:
BETWEEN述語は範囲の検査を次のように単純にします。
aBETWEENxANDy
is equivalent to は
a>=xANDa<=y
と同じです。
Notice that <token>BETWEEN</token> treats the endpoint values as included
in the range.
<literal>BETWEEN SYMMETRIC</literal> is like <literal>BETWEEN</literal>
except there is no requirement that the argument to the left of
<literal>AND</literal> be less than or equal to the argument on the right.
If it is not, those two arguments are automatically swapped, so that
a nonempty range is always implied.
BETWEENは範囲内に含まれるとして端点値を扱うことに注意してください。
BETWEEN SYMMETRICは、ANDの左側の引数が右側の引数より小さいか、もしくは等しいという必要性が無い点を除きBETWEENと同様です。
この条件を満たしていない場合、2つの引数は自動的に交換されますので、常に空ではない範囲となります。
The various variants of <literal>BETWEEN</literal> are implemented in
terms of the ordinary comparison operators, and therefore will work for
any data type(s) that can be compared.
BETWEENの変種は通常の比較演算子を使って実装されており、比較可能なすべてのデータ型に対して使用できます。
The use of <literal>AND</literal> in the <literal>BETWEEN</literal>
syntax creates an ambiguity with the use of <literal>AND</literal> as a
logical operator. To resolve this, only a limited set of expression
types are allowed as the second argument of a <literal>BETWEEN</literal>
clause. If you need to write a more complex sub-expression
in <literal>BETWEEN</literal>, write parentheses around the
sub-expression.
BETWEEN構文中でANDを使用すると、ANDを論理演算子として使うこととの曖昧さが生じます。
これを解決するために、BETWEEN句の第2引数としては限定された式の種類のみが利用できます。
BETWEEN中で複雑な副式を使用する必要がある場合は、副式を括弧で囲んでください。
Ordinary comparison operators yield null (signifying <quote>unknown</quote>),
not true or false, when either input is null. For example,
<literal>7 = NULL</literal> yields null, as does <literal>7 <> NULL</literal>. When
this behavior is not suitable, use the
<literal>IS <optional> NOT </optional> DISTINCT FROM</literal> predicates:
入力のどちらかがNULLの場合、通常の比較演算子は真や偽ではなく(「不明」を意味する)nullを生成します。
例えば7 = NULLはnullになります。7 <> NULLも同様です。
この動作が適切でない場合は、IS [ NOT ] DISTINCT FROM述語を使用してください。
aIS DISTINCT FROMbaIS NOT DISTINCT FROMb
For non-null inputs, <literal>IS DISTINCT FROM</literal> is
the same as the <literal><></literal> operator. However, if both
inputs are null it returns false, and if only one input is
null it returns true. Similarly, <literal>IS NOT DISTINCT
FROM</literal> is identical to <literal>=</literal> for non-null
inputs, but it returns true when both inputs are null, and false when only
one input is null. Thus, these predicates effectively act as though null
were a normal data value, rather than <quote>unknown</quote>.
非NULLの入力では、IS DISTINCT FROMは<>演算子と同じです。
しかし、入力がどちらもNULLの場合、これは偽を返し、片方の入力のみがNULLの場合は真を返します。
同様に、IS NOT DISTINCT FROMは非NULL入力では=と同じですが、両方の入力がNULLであれば真を、片方のみがNULLの場合は偽を返します。
このように、これらの述語はNULLを「不明な値」ではなく、通常の値かのように動作します。
To check whether a value is or is not null, use the predicates: 値がNULLかNULLでないかを検証するには次の述語を使います。
expressionIS NULLexpressionIS NOT NULL
or the equivalent, but nonstandard, predicates: あるいは、これと同等の、非標準の述語も使えます。
expressionISNULLexpressionNOTNULL
Do <emphasis>not</emphasis> write
<literal><replaceable>expression</replaceable> = NULL</literal>
because <literal>NULL</literal> is not <quote>equal to</quote>
<literal>NULL</literal>. (The null value represents an unknown value,
and it is not known whether two unknown values are equal.)
NULLとNULLとは「等しい」関係にはありませんので、と記述してはいけません。
(NULL値は不明の値を表しているため、不明な値同士が同じかどうかは識別できません。)
expression = NULL
Some applications might expect that
<literal><replaceable>expression</replaceable> = NULL</literal>
returns true if <replaceable>expression</replaceable> evaluates to
the null value. It is highly recommended that these applications
be modified to comply with the SQL standard. However, if that
cannot be done the <xref linkend="guc-transform-null-equals"/>
configuration variable is available. If it is enabled,
<productname>PostgreSQL</productname> will convert <literal>x =
NULL</literal> clauses to <literal>x IS NULL</literal>.
アプリケーションによっては、が、expression = NULLexpressionがNULL値と評価されるのであれば真を返すことを期待することがあります。
これらのアプリケーションを標準SQLに準拠するように変更することを強くお勧めします。
しかし、それができなければtransform_null_equalsを使用することで対応することができます。
これを有効にした場合、PostgreSQLはx = NULL句をx IS NULLに変換します。
If the <replaceable>expression</replaceable> is row-valued, then
<literal>IS NULL</literal> is true when the row expression itself is null
or when all the row's fields are null, while
<literal>IS NOT NULL</literal> is true when the row expression itself is non-null
and all the row's fields are non-null. Because of this behavior,
<literal>IS NULL</literal> and <literal>IS NOT NULL</literal> do not always return
inverse results for row-valued expressions; in particular, a row-valued
expression that contains both null and non-null fields will return false
for both tests. For example:
expressionが行値の場合、行式自体がNULLまたは、行のフィールドすべてがNULLの場合にIS NULLは真となります。
一方IS NOT NULLは、行式自体が非NULLかつ、行のフィールドすべてが非NULLの場合に真となります。
この動作により、IS NULLおよびIS NOT NULLは行値評価式に対し常に反対の結果を返すわけではありません。
特に、NULLと非NULLの値の両方を含む行値式はどちらの試験でも偽を返します。
たとえば、
SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same'); SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows SELECT ROW(table.*) IS NOT NULL FROM table; -- detect all-non-null rows SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in rows
In some cases, it may be preferable to
write <replaceable>row</replaceable> <literal>IS DISTINCT FROM NULL</literal>
or <replaceable>row</replaceable> <literal>IS NOT DISTINCT FROM NULL</literal>,
which will simply check whether the overall row value is null without any
additional tests on the row fields.
場合によっては、row IS DISTINCT FROM NULLあるいはrow IS NOT DISTINCT FROM NULLと記述する方が望ましいことがあるでしょう。
これらは単に行全体の値がNULLかどうかを検査し、行のフィールドについての追加的検査を全く行わないからです。
Boolean values can also be tested using the predicates 論理値も次の述語で検証できます。
boolean_expressionIS TRUEboolean_expressionIS NOT TRUEboolean_expressionIS FALSEboolean_expressionIS NOT FALSEboolean_expressionIS UNKNOWNboolean_expressionIS NOT UNKNOWN
These will always return true or false, never a null value, even when the
operand is null.
A null input is treated as the logical value <quote>unknown</quote>.
Notice that <literal>IS UNKNOWN</literal> and <literal>IS NOT UNKNOWN</literal> are
effectively the same as <literal>IS NULL</literal> and
<literal>IS NOT NULL</literal>, respectively, except that the input
expression must be of Boolean type.
これらは、常に真か偽を返し、演算項目がNULLであってもNULL値を返すことはありません。
NULL値が入力されると、「不明」という論理値として扱われます。
IS UNKNOWNとIS NOT UNKNOWNが、入力式が論理値型でなければならないという点を除き、それぞれ実質的にIS NULLとIS NOT NULLと同じであることに注意してください。
Some comparison-related functions are also available, as shown in <xref linkend="functions-comparison-func-table"/>. 表 9.3に示すように、比較に関連した関数がいくつか使用可能です。
表9.3 比較関数