gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

213 行
17KB

  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.23. Row and Array Comparisons</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-subquery.html" title="9.22. Subquery Expressions" /><link rel="next" href="functions-srf.html" title="9.24. Set Returning Functions" /></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.23. Row and Array Comparisons</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-subquery.html" title="9.22. Subquery Expressions">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-srf.html" title="9.24. Set Returning Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-COMPARISONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.23. Row and Array Comparisons</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="functions-comparisons.html#FUNCTIONS-COMPARISONS-IN-SCALAR">9.23.1. <code class="literal">IN</code></a></span></dt><dt><span class="sect2"><a href="functions-comparisons.html#id-1.5.8.28.15">9.23.2. <code class="literal">NOT IN</code></a></span></dt><dt><span class="sect2"><a href="functions-comparisons.html#id-1.5.8.28.16">9.23.3. <code class="literal">ANY</code>/<code class="literal">SOME</code> (array)</a></span></dt><dt><span class="sect2"><a href="functions-comparisons.html#id-1.5.8.28.17">9.23.4. <code class="literal">ALL</code> (array)</a></span></dt><dt><span class="sect2"><a href="functions-comparisons.html#ROW-WISE-COMPARISON">9.23.5. Row Constructor Comparison</a></span></dt><dt><span class="sect2"><a href="functions-comparisons.html#COMPOSITE-TYPE-COMPARISON">9.23.6. Composite Type Comparison</a></span></dt></dl></div><a id="id-1.5.8.28.2" class="indexterm"></a><a id="id-1.5.8.28.3" class="indexterm"></a><a id="id-1.5.8.28.4" class="indexterm"></a><a id="id-1.5.8.28.5" class="indexterm"></a><a id="id-1.5.8.28.6" class="indexterm"></a><a id="id-1.5.8.28.7" class="indexterm"></a><a id="id-1.5.8.28.8" class="indexterm"></a><a id="id-1.5.8.28.9" class="indexterm"></a><a id="id-1.5.8.28.10" class="indexterm"></a><a id="id-1.5.8.28.11" class="indexterm"></a><a id="id-1.5.8.28.12" class="indexterm"></a><p>
  3. This section describes several specialized constructs for making
  4. multiple comparisons between groups of values. These forms are
  5. syntactically related to the subquery forms of the previous section,
  6. but do not involve subqueries.
  7. The forms involving array subexpressions are
  8. <span class="productname">PostgreSQL</span> extensions; the rest are
  9. <acronym class="acronym">SQL</acronym>-compliant.
  10. All of the expression forms documented in this section return
  11. Boolean (true/false) results.
  12. </p><div class="sect2" id="FUNCTIONS-COMPARISONS-IN-SCALAR"><div class="titlepage"><div><div><h3 class="title">9.23.1. <code class="literal">IN</code></h3></div></div></div><pre class="synopsis">
  13. <em class="replaceable"><code>expression</code></em> IN (<em class="replaceable"><code>value</code></em> [<span class="optional">, ...</span>])
  14. </pre><p>
  15. The right-hand side is a parenthesized list
  16. of scalar expressions. The result is <span class="quote">“<span class="quote">true</span>”</span> if the left-hand expression's
  17. result is equal to any of the right-hand expressions. This is a shorthand
  18. notation for
  19. </p><pre class="synopsis">
  20. <em class="replaceable"><code>expression</code></em> = <em class="replaceable"><code>value1</code></em>
  21. OR
  22. <em class="replaceable"><code>expression</code></em> = <em class="replaceable"><code>value2</code></em>
  23. OR
  24. ...
  25. </pre><p>
  26. </p><p>
  27. Note that if the left-hand expression yields null, or if there are
  28. no equal right-hand values and at least one right-hand expression yields
  29. null, the result of the <code class="token">IN</code> construct will be null, not false.
  30. This is in accordance with SQL's normal rules for Boolean combinations
  31. of null values.
  32. </p></div><div class="sect2" id="id-1.5.8.28.15"><div class="titlepage"><div><div><h3 class="title">9.23.2. <code class="literal">NOT IN</code></h3></div></div></div><pre class="synopsis">
  33. <em class="replaceable"><code>expression</code></em> NOT IN (<em class="replaceable"><code>value</code></em> [<span class="optional">, ...</span>])
  34. </pre><p>
  35. The right-hand side is a parenthesized list
  36. of scalar expressions. The result is <span class="quote">“<span class="quote">true</span>”</span> if the left-hand expression's
  37. result is unequal to all of the right-hand expressions. This is a shorthand
  38. notation for
  39. </p><pre class="synopsis">
  40. <em class="replaceable"><code>expression</code></em> &lt;&gt; <em class="replaceable"><code>value1</code></em>
  41. AND
  42. <em class="replaceable"><code>expression</code></em> &lt;&gt; <em class="replaceable"><code>value2</code></em>
  43. AND
  44. ...
  45. </pre><p>
  46. </p><p>
  47. Note that if the left-hand expression yields null, or if there are
  48. no equal right-hand values and at least one right-hand expression yields
  49. null, the result of the <code class="token">NOT IN</code> construct will be null, not true
  50. as one might naively expect.
  51. This is in accordance with SQL's normal rules for Boolean combinations
  52. of null values.
  53. </p><div class="tip"><h3 class="title">Tip</h3><p>
  54. <code class="literal">x NOT IN y</code> is equivalent to <code class="literal">NOT (x IN y)</code> in all
  55. cases. However, null values are much more likely to trip up the novice when
  56. working with <code class="token">NOT IN</code> than when working with <code class="token">IN</code>.
  57. It is best to express your condition positively if possible.
  58. </p></div></div><div class="sect2" id="id-1.5.8.28.16"><div class="titlepage"><div><div><h3 class="title">9.23.3. <code class="literal">ANY</code>/<code class="literal">SOME</code> (array)</h3></div></div></div><pre class="synopsis">
  59. <em class="replaceable"><code>expression</code></em> <em class="replaceable"><code>operator</code></em> ANY (<em class="replaceable"><code>array expression</code></em>)
  60. <em class="replaceable"><code>expression</code></em> <em class="replaceable"><code>operator</code></em> SOME (<em class="replaceable"><code>array expression</code></em>)
  61. </pre><p>
  62. The right-hand side is a parenthesized expression, which must yield an
  63. array value.
  64. The left-hand expression
  65. is evaluated and compared to each element of the array using the
  66. given <em class="replaceable"><code>operator</code></em>, which must yield a Boolean
  67. result.
  68. The result of <code class="token">ANY</code> is <span class="quote">“<span class="quote">true</span>”</span> if any true result is obtained.
  69. The result is <span class="quote">“<span class="quote">false</span>”</span> if no true result is found (including the
  70. case where the array has zero elements).
  71. </p><p>
  72. If the array expression yields a null array, the result of
  73. <code class="token">ANY</code> will be null. If the left-hand expression yields null,
  74. the result of <code class="token">ANY</code> is ordinarily null (though a non-strict
  75. comparison operator could possibly yield a different result).
  76. Also, if the right-hand array contains any null elements and no true
  77. comparison result is obtained, the result of <code class="token">ANY</code>
  78. will be null, not false (again, assuming a strict comparison operator).
  79. This is in accordance with SQL's normal rules for Boolean combinations
  80. of null values.
  81. </p><p>
  82. <code class="token">SOME</code> is a synonym for <code class="token">ANY</code>.
  83. </p></div><div class="sect2" id="id-1.5.8.28.17"><div class="titlepage"><div><div><h3 class="title">9.23.4. <code class="literal">ALL</code> (array)</h3></div></div></div><pre class="synopsis">
  84. <em class="replaceable"><code>expression</code></em> <em class="replaceable"><code>operator</code></em> ALL (<em class="replaceable"><code>array expression</code></em>)
  85. </pre><p>
  86. The right-hand side is a parenthesized expression, which must yield an
  87. array value.
  88. The left-hand expression
  89. is evaluated and compared to each element of the array using the
  90. given <em class="replaceable"><code>operator</code></em>, which must yield a Boolean
  91. result.
  92. The result of <code class="token">ALL</code> is <span class="quote">“<span class="quote">true</span>”</span> if all comparisons yield true
  93. (including the case where the array has zero elements).
  94. The result is <span class="quote">“<span class="quote">false</span>”</span> if any false result is found.
  95. </p><p>
  96. If the array expression yields a null array, the result of
  97. <code class="token">ALL</code> will be null. If the left-hand expression yields null,
  98. the result of <code class="token">ALL</code> is ordinarily null (though a non-strict
  99. comparison operator could possibly yield a different result).
  100. Also, if the right-hand array contains any null elements and no false
  101. comparison result is obtained, the result of <code class="token">ALL</code>
  102. will be null, not true (again, assuming a strict comparison operator).
  103. This is in accordance with SQL's normal rules for Boolean combinations
  104. of null values.
  105. </p></div><div class="sect2" id="ROW-WISE-COMPARISON"><div class="titlepage"><div><div><h3 class="title">9.23.5. Row Constructor Comparison</h3></div></div></div><pre class="synopsis">
  106. <em class="replaceable"><code>row_constructor</code></em> <em class="replaceable"><code>operator</code></em> <em class="replaceable"><code>row_constructor</code></em>
  107. </pre><p>
  108. Each side is a row constructor,
  109. as described in <a class="xref" href="sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS" title="4.2.13. Row Constructors">Section 4.2.13</a>.
  110. The two row values must have the same number of fields.
  111. Each side is evaluated and they are compared row-wise. Row constructor
  112. comparisons are allowed when the <em class="replaceable"><code>operator</code></em> is
  113. <code class="literal">=</code>,
  114. <code class="literal">&lt;&gt;</code>,
  115. <code class="literal">&lt;</code>,
  116. <code class="literal">&lt;=</code>,
  117. <code class="literal">&gt;</code> or
  118. <code class="literal">&gt;=</code>.
  119. Every row element must be of a type which has a default B-tree operator
  120. class or the attempted comparison may generate an error.
  121. </p><div class="note"><h3 class="title">Note</h3><p>
  122. Errors related to the number or types of elements might not occur if
  123. the comparison is resolved using earlier columns.
  124. </p></div><p>
  125. The <code class="literal">=</code> and <code class="literal">&lt;&gt;</code> cases work slightly differently
  126. from the others. Two rows are considered
  127. equal if all their corresponding members are non-null and equal; the rows
  128. are unequal if any corresponding members are non-null and unequal;
  129. otherwise the result of the row comparison is unknown (null).
  130. </p><p>
  131. For the <code class="literal">&lt;</code>, <code class="literal">&lt;=</code>, <code class="literal">&gt;</code> and
  132. <code class="literal">&gt;=</code> cases, the row elements are compared left-to-right,
  133. stopping as soon as an unequal or null pair of elements is found.
  134. If either of this pair of elements is null, the result of the
  135. row comparison is unknown (null); otherwise comparison of this pair
  136. of elements determines the result. For example,
  137. <code class="literal">ROW(1,2,NULL) &lt; ROW(1,3,0)</code>
  138. yields true, not null, because the third pair of elements are not
  139. considered.
  140. </p><div class="note"><h3 class="title">Note</h3><p>
  141. Prior to <span class="productname">PostgreSQL</span> 8.2, the
  142. <code class="literal">&lt;</code>, <code class="literal">&lt;=</code>, <code class="literal">&gt;</code> and <code class="literal">&gt;=</code>
  143. cases were not handled per SQL specification. A comparison like
  144. <code class="literal">ROW(a,b) &lt; ROW(c,d)</code>
  145. was implemented as
  146. <code class="literal">a &lt; c AND b &lt; d</code>
  147. whereas the correct behavior is equivalent to
  148. <code class="literal">a &lt; c OR (a = c AND b &lt; d)</code>.
  149. </p></div><pre class="synopsis">
  150. <em class="replaceable"><code>row_constructor</code></em> IS DISTINCT FROM <em class="replaceable"><code>row_constructor</code></em>
  151. </pre><p>
  152. This construct is similar to a <code class="literal">&lt;&gt;</code> row comparison,
  153. but it does not yield null for null inputs. Instead, any null value is
  154. considered unequal to (distinct from) any non-null value, and any two
  155. nulls are considered equal (not distinct). Thus the result will
  156. either be true or false, never null.
  157. </p><pre class="synopsis">
  158. <em class="replaceable"><code>row_constructor</code></em> IS NOT DISTINCT FROM <em class="replaceable"><code>row_constructor</code></em>
  159. </pre><p>
  160. This construct is similar to a <code class="literal">=</code> row comparison,
  161. but it does not yield null for null inputs. Instead, any null value is
  162. considered unequal to (distinct from) any non-null value, and any two
  163. nulls are considered equal (not distinct). Thus the result will always
  164. be either true or false, never null.
  165. </p></div><div class="sect2" id="COMPOSITE-TYPE-COMPARISON"><div class="titlepage"><div><div><h3 class="title">9.23.6. Composite Type Comparison</h3></div></div></div><pre class="synopsis">
  166. <em class="replaceable"><code>record</code></em> <em class="replaceable"><code>operator</code></em> <em class="replaceable"><code>record</code></em>
  167. </pre><p>
  168. The SQL specification requires row-wise comparison to return NULL if the
  169. result depends on comparing two NULL values or a NULL and a non-NULL.
  170. <span class="productname">PostgreSQL</span> does this only when comparing the
  171. results of two row constructors (as in
  172. <a class="xref" href="functions-comparisons.html#ROW-WISE-COMPARISON" title="9.23.5. Row Constructor Comparison">Section 9.23.5</a>) or comparing a row constructor
  173. to the output of a subquery (as in <a class="xref" href="functions-subquery.html" title="9.22. Subquery Expressions">Section 9.22</a>).
  174. In other contexts where two composite-type values are compared, two
  175. NULL field values are considered equal, and a NULL is considered larger
  176. than a non-NULL. This is necessary in order to have consistent sorting
  177. and indexing behavior for composite types.
  178. </p><p>
  179. Each side is evaluated and they are compared row-wise. Composite type
  180. comparisons are allowed when the <em class="replaceable"><code>operator</code></em> is
  181. <code class="literal">=</code>,
  182. <code class="literal">&lt;&gt;</code>,
  183. <code class="literal">&lt;</code>,
  184. <code class="literal">&lt;=</code>,
  185. <code class="literal">&gt;</code> or
  186. <code class="literal">&gt;=</code>,
  187. or has semantics similar to one of these. (To be specific, an operator
  188. can be a row comparison operator if it is a member of a B-tree operator
  189. class, or is the negator of the <code class="literal">=</code> member of a B-tree operator
  190. class.) The default behavior of the above operators is the same as for
  191. <code class="literal">IS [ NOT ] DISTINCT FROM</code> for row constructors (see
  192. <a class="xref" href="functions-comparisons.html#ROW-WISE-COMPARISON" title="9.23.5. Row Constructor Comparison">Section 9.23.5</a>).
  193. </p><p>
  194. To support matching of rows which include elements without a default
  195. B-tree operator class, the following operators are defined for composite
  196. type comparison:
  197. <code class="literal">*=</code>,
  198. <code class="literal">*&lt;&gt;</code>,
  199. <code class="literal">*&lt;</code>,
  200. <code class="literal">*&lt;=</code>,
  201. <code class="literal">*&gt;</code>, and
  202. <code class="literal">*&gt;=</code>.
  203. These operators compare the internal binary representation of the two
  204. rows. Two rows might have a different binary representation even
  205. though comparisons of the two rows with the equality operator is true.
  206. The ordering of rows under these comparison operators is deterministic
  207. but not otherwise meaningful. These operators are used internally for
  208. materialized views and might be useful for other specialized purposes
  209. such as replication but are not intended to be generally useful for
  210. writing queries.
  211. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-subquery.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-srf.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.22. Subquery Expressions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.24. Set Returning Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1