gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
16KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.2. Comparison Functions and Operators</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="functions-logical.html" title="9.1. Logical Operators" /><link rel="next" href="functions-math.html" title="9.3. Mathematical Functions and Operators" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.2. Comparison Functions and Operators</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-logical.html" title="9.1. Logical Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-math.html" title="9.3. Mathematical Functions and Operators">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-COMPARISON"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.2. Comparison Functions and Operators</h2></div></div></div><a id="id-1.5.8.7.2" class="indexterm"></a><p>
  3. The usual comparison operators are available, as shown in <a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE" title="Table 9.1. Comparison Operators">Table 9.1</a>.
  4. </p><div class="table" id="FUNCTIONS-COMPARISON-OP-TABLE"><p class="title"><strong>Table 9.1. Comparison Operators</strong></p><div class="table-contents"><table class="table" summary="Comparison Operators" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Operator</th><th>Description</th></tr></thead><tbody><tr><td> <code class="literal">&lt;</code> </td><td>less than</td></tr><tr><td> <code class="literal">&gt;</code> </td><td>greater than</td></tr><tr><td> <code class="literal">&lt;=</code> </td><td>less than or equal to</td></tr><tr><td> <code class="literal">&gt;=</code> </td><td>greater than or equal to</td></tr><tr><td> <code class="literal">=</code> </td><td>equal</td></tr><tr><td> <code class="literal">&lt;&gt;</code> or <code class="literal">!=</code> </td><td>not equal</td></tr></tbody></table></div></div><br class="table-break" /><div class="note"><h3 class="title">Note</h3><p>
  5. The <code class="literal">!=</code> operator is converted to
  6. <code class="literal">&lt;&gt;</code> in the parser stage. It is not
  7. possible to implement <code class="literal">!=</code> and
  8. <code class="literal">&lt;&gt;</code> operators that do different things.
  9. </p></div><p>
  10. Comparison operators are available for all relevant data types.
  11. All comparison operators are binary operators that
  12. return values of type <code class="type">boolean</code>; expressions like
  13. <code class="literal">1 &lt; 2 &lt; 3</code> are not valid (because there is
  14. no <code class="literal">&lt;</code> operator to compare a Boolean value with
  15. <code class="literal">3</code>).
  16. </p><p>
  17. There are also some comparison predicates, as shown in <a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-PRED-TABLE" title="Table 9.2. Comparison Predicates">Table 9.2</a>. These behave much like
  18. operators, but have special syntax mandated by the SQL standard.
  19. </p><div class="table" id="FUNCTIONS-COMPARISON-PRED-TABLE"><p class="title"><strong>Table 9.2. Comparison Predicates</strong></p><div class="table-contents"><table class="table" summary="Comparison Predicates" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Predicate</th><th>Description</th></tr></thead><tbody><tr><td> <em class="replaceable"><code>a</code></em> <code class="literal">BETWEEN</code> <em class="replaceable"><code>x</code></em> <code class="literal">AND</code> <em class="replaceable"><code>y</code></em> </td><td>between</td></tr><tr><td> <em class="replaceable"><code>a</code></em> <code class="literal">NOT BETWEEN</code> <em class="replaceable"><code>x</code></em> <code class="literal">AND</code> <em class="replaceable"><code>y</code></em> </td><td>not between</td></tr><tr><td> <em class="replaceable"><code>a</code></em> <code class="literal">BETWEEN SYMMETRIC</code> <em class="replaceable"><code>x</code></em> <code class="literal">AND</code> <em class="replaceable"><code>y</code></em> </td><td>between, after sorting the comparison values</td></tr><tr><td> <em class="replaceable"><code>a</code></em> <code class="literal">NOT BETWEEN SYMMETRIC</code> <em class="replaceable"><code>x</code></em> <code class="literal">AND</code> <em class="replaceable"><code>y</code></em> </td><td>not between, after sorting the comparison values</td></tr><tr><td> <em class="replaceable"><code>a</code></em> <code class="literal">IS DISTINCT FROM</code> <em class="replaceable"><code>b</code></em> </td><td>not equal, treating null like an ordinary value</td></tr><tr><td><em class="replaceable"><code>a</code></em> <code class="literal">IS NOT DISTINCT FROM</code> <em class="replaceable"><code>b</code></em></td><td>equal, treating null like an ordinary value</td></tr><tr><td> <em class="replaceable"><code>expression</code></em> <code class="literal">IS NULL</code> </td><td>is null</td></tr><tr><td> <em class="replaceable"><code>expression</code></em> <code class="literal">IS NOT NULL</code> </td><td>is not null</td></tr><tr><td> <em class="replaceable"><code>expression</code></em> <code class="literal">ISNULL</code> </td><td>is null (nonstandard syntax)</td></tr><tr><td> <em class="replaceable"><code>expression</code></em> <code class="literal">NOTNULL</code> </td><td>is not null (nonstandard syntax)</td></tr><tr><td> <em class="replaceable"><code>boolean_expression</code></em> <code class="literal">IS TRUE</code> </td><td>is true</td></tr><tr><td> <em class="replaceable"><code>boolean_expression</code></em> <code class="literal">IS NOT TRUE</code> </td><td>is false or unknown</td></tr><tr><td> <em class="replaceable"><code>boolean_expression</code></em> <code class="literal">IS FALSE</code> </td><td>is false</td></tr><tr><td> <em class="replaceable"><code>boolean_expression</code></em> <code class="literal">IS NOT FALSE</code> </td><td>is true or unknown</td></tr><tr><td> <em class="replaceable"><code>boolean_expression</code></em> <code class="literal">IS UNKNOWN</code> </td><td>is unknown</td></tr><tr><td> <em class="replaceable"><code>boolean_expression</code></em> <code class="literal">IS NOT UNKNOWN</code> </td><td>is true or false</td></tr></tbody></table></div></div><br class="table-break" /><p>
  20. <a id="id-1.5.8.7.9.1" class="indexterm"></a>
  21. The <code class="token">BETWEEN</code> predicate simplifies range tests:
  22. </p><pre class="synopsis">
  23. <em class="replaceable"><code>a</code></em> BETWEEN <em class="replaceable"><code>x</code></em> AND <em class="replaceable"><code>y</code></em>
  24. </pre><p>
  25. is equivalent to
  26. </p><pre class="synopsis">
  27. <em class="replaceable"><code>a</code></em> &gt;= <em class="replaceable"><code>x</code></em> AND <em class="replaceable"><code>a</code></em> &lt;= <em class="replaceable"><code>y</code></em>
  28. </pre><p>
  29. Notice that <code class="token">BETWEEN</code> treats the endpoint values as included
  30. in the range.
  31. <code class="literal">NOT BETWEEN</code> does the opposite comparison:
  32. </p><pre class="synopsis">
  33. <em class="replaceable"><code>a</code></em> NOT BETWEEN <em class="replaceable"><code>x</code></em> AND <em class="replaceable"><code>y</code></em>
  34. </pre><p>
  35. is equivalent to
  36. </p><pre class="synopsis">
  37. <em class="replaceable"><code>a</code></em> &lt; <em class="replaceable"><code>x</code></em> OR <em class="replaceable"><code>a</code></em> &gt; <em class="replaceable"><code>y</code></em>
  38. </pre><p>
  39. <a id="id-1.5.8.7.9.9" class="indexterm"></a>
  40. <code class="literal">BETWEEN SYMMETRIC</code> is like <code class="literal">BETWEEN</code>
  41. except there is no requirement that the argument to the left of
  42. <code class="literal">AND</code> be less than or equal to the argument on the right.
  43. If it is not, those two arguments are automatically swapped, so that
  44. a nonempty range is always implied.
  45. </p><p>
  46. <a id="id-1.5.8.7.10.1" class="indexterm"></a>
  47. <a id="id-1.5.8.7.10.2" class="indexterm"></a>
  48. Ordinary comparison operators yield null (signifying <span class="quote">“<span class="quote">unknown</span>”</span>),
  49. not true or false, when either input is null. For example,
  50. <code class="literal">7 = NULL</code> yields null, as does <code class="literal">7 &lt;&gt; NULL</code>. When
  51. this behavior is not suitable, use the
  52. <code class="literal">IS [<span class="optional"> NOT </span>] DISTINCT FROM</code> predicates:
  53. </p><pre class="synopsis">
  54. <em class="replaceable"><code>a</code></em> IS DISTINCT FROM <em class="replaceable"><code>b</code></em>
  55. <em class="replaceable"><code>a</code></em> IS NOT DISTINCT FROM <em class="replaceable"><code>b</code></em>
  56. </pre><p>
  57. For non-null inputs, <code class="literal">IS DISTINCT FROM</code> is
  58. the same as the <code class="literal">&lt;&gt;</code> operator. However, if both
  59. inputs are null it returns false, and if only one input is
  60. null it returns true. Similarly, <code class="literal">IS NOT DISTINCT
  61. FROM</code> is identical to <code class="literal">=</code> for non-null
  62. inputs, but it returns true when both inputs are null, and false when only
  63. one input is null. Thus, these predicates effectively act as though null
  64. were a normal data value, rather than <span class="quote">“<span class="quote">unknown</span>”</span>.
  65. </p><p>
  66. <a id="id-1.5.8.7.11.1" class="indexterm"></a>
  67. <a id="id-1.5.8.7.11.2" class="indexterm"></a>
  68. <a id="id-1.5.8.7.11.3" class="indexterm"></a>
  69. <a id="id-1.5.8.7.11.4" class="indexterm"></a>
  70. To check whether a value is or is not null, use the predicates:
  71. </p><pre class="synopsis">
  72. <em class="replaceable"><code>expression</code></em> IS NULL
  73. <em class="replaceable"><code>expression</code></em> IS NOT NULL
  74. </pre><p>
  75. or the equivalent, but nonstandard, predicates:
  76. </p><pre class="synopsis">
  77. <em class="replaceable"><code>expression</code></em> ISNULL
  78. <em class="replaceable"><code>expression</code></em> NOTNULL
  79. </pre><p>
  80. <a id="id-1.5.8.7.11.7" class="indexterm"></a>
  81. </p><p>
  82. Do <span class="emphasis"><em>not</em></span> write
  83. <code class="literal"><em class="replaceable"><code>expression</code></em> = NULL</code>
  84. because <code class="literal">NULL</code> is not <span class="quote">“<span class="quote">equal to</span>”</span>
  85. <code class="literal">NULL</code>. (The null value represents an unknown value,
  86. and it is not known whether two unknown values are equal.)
  87. </p><div class="tip"><h3 class="title">Tip</h3><p>
  88. Some applications might expect that
  89. <code class="literal"><em class="replaceable"><code>expression</code></em> = NULL</code>
  90. returns true if <em class="replaceable"><code>expression</code></em> evaluates to
  91. the null value. It is highly recommended that these applications
  92. be modified to comply with the SQL standard. However, if that
  93. cannot be done the <a class="xref" href="runtime-config-compatible.html#GUC-TRANSFORM-NULL-EQUALS">transform_null_equals</a>
  94. configuration variable is available. If it is enabled,
  95. <span class="productname">PostgreSQL</span> will convert <code class="literal">x =
  96. NULL</code> clauses to <code class="literal">x IS NULL</code>.
  97. </p></div><p>
  98. If the <em class="replaceable"><code>expression</code></em> is row-valued, then
  99. <code class="literal">IS NULL</code> is true when the row expression itself is null
  100. or when all the row's fields are null, while
  101. <code class="literal">IS NOT NULL</code> is true when the row expression itself is non-null
  102. and all the row's fields are non-null. Because of this behavior,
  103. <code class="literal">IS NULL</code> and <code class="literal">IS NOT NULL</code> do not always return
  104. inverse results for row-valued expressions; in particular, a row-valued
  105. expression that contains both null and non-null fields will return false
  106. for both tests. In some cases, it may be preferable to
  107. write <em class="replaceable"><code>row</code></em> <code class="literal">IS DISTINCT FROM NULL</code>
  108. or <em class="replaceable"><code>row</code></em> <code class="literal">IS NOT DISTINCT FROM NULL</code>,
  109. which will simply check whether the overall row value is null without any
  110. additional tests on the row fields.
  111. </p><p>
  112. <a id="id-1.5.8.7.15.1" class="indexterm"></a>
  113. <a id="id-1.5.8.7.15.2" class="indexterm"></a>
  114. <a id="id-1.5.8.7.15.3" class="indexterm"></a>
  115. <a id="id-1.5.8.7.15.4" class="indexterm"></a>
  116. <a id="id-1.5.8.7.15.5" class="indexterm"></a>
  117. <a id="id-1.5.8.7.15.6" class="indexterm"></a>
  118. Boolean values can also be tested using the predicates
  119. </p><pre class="synopsis">
  120. <em class="replaceable"><code>boolean_expression</code></em> IS TRUE
  121. <em class="replaceable"><code>boolean_expression</code></em> IS NOT TRUE
  122. <em class="replaceable"><code>boolean_expression</code></em> IS FALSE
  123. <em class="replaceable"><code>boolean_expression</code></em> IS NOT FALSE
  124. <em class="replaceable"><code>boolean_expression</code></em> IS UNKNOWN
  125. <em class="replaceable"><code>boolean_expression</code></em> IS NOT UNKNOWN
  126. </pre><p>
  127. These will always return true or false, never a null value, even when the
  128. operand is null.
  129. A null input is treated as the logical value <span class="quote">“<span class="quote">unknown</span>”</span>.
  130. Notice that <code class="literal">IS UNKNOWN</code> and <code class="literal">IS NOT UNKNOWN</code> are
  131. effectively the same as <code class="literal">IS NULL</code> and
  132. <code class="literal">IS NOT NULL</code>, respectively, except that the input
  133. expression must be of Boolean type.
  134. </p><p>
  135. Some comparison-related functions are also available, as shown in <a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-FUNC-TABLE" title="Table 9.3. Comparison Functions">Table 9.3</a>.
  136. </p><div class="table" id="FUNCTIONS-COMPARISON-FUNC-TABLE"><p class="title"><strong>Table 9.3. Comparison Functions</strong></p><div class="table-contents"><table class="table" summary="Comparison Functions" border="1"><colgroup><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Description</th><th>Example</th><th>Example Result</th></tr></thead><tbody><tr><td>
  137. <a id="id-1.5.8.7.17.2.2.1.1.1" class="indexterm"></a>
  138. <code class="literal">num_nonnulls(VARIADIC "any")</code>
  139. </td><td>returns the number of non-null arguments</td><td><code class="literal">num_nonnulls(1, NULL, 2)</code></td><td><code class="literal">2</code></td></tr><tr><td>
  140. <a id="id-1.5.8.7.17.2.2.2.1.1" class="indexterm"></a>
  141. <code class="literal">num_nulls(VARIADIC "any")</code>
  142. </td><td>returns the number of null arguments</td><td><code class="literal">num_nulls(1, NULL, 2)</code></td><td><code class="literal">1</code></td></tr></tbody></table></div></div><br class="table-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-logical.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-math.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.1. Logical Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.3. Mathematical Functions and Operators</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1