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

169 行
12KB

  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.17. Conditional Expressions</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-sequence.html" title="9.16. Sequence Manipulation Functions" /><link rel="next" href="functions-array.html" title="9.18. Array 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.17. Conditional Expressions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-sequence.html" title="9.16. Sequence Manipulation Functions">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-array.html" title="9.18. Array Functions and Operators">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-CONDITIONAL"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.17. Conditional Expressions</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="functions-conditional.html#FUNCTIONS-CASE">9.17.1. <code class="literal">CASE</code></a></span></dt><dt><span class="sect2"><a href="functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL">9.17.2. <code class="literal">COALESCE</code></a></span></dt><dt><span class="sect2"><a href="functions-conditional.html#FUNCTIONS-NULLIF">9.17.3. <code class="literal">NULLIF</code></a></span></dt><dt><span class="sect2"><a href="functions-conditional.html#FUNCTIONS-GREATEST-LEAST">9.17.4. <code class="literal">GREATEST</code> and <code class="literal">LEAST</code></a></span></dt></dl></div><a id="id-1.5.8.22.2" class="indexterm"></a><a id="id-1.5.8.22.3" class="indexterm"></a><p>
  3. This section describes the <acronym class="acronym">SQL</acronym>-compliant conditional expressions
  4. available in <span class="productname">PostgreSQL</span>.
  5. </p><div class="tip"><h3 class="title">Tip</h3><p>
  6. If your needs go beyond the capabilities of these conditional
  7. expressions, you might want to consider writing a server-side function
  8. in a more expressive programming language.
  9. </p></div><div class="note"><h3 class="title">Note</h3><p>
  10. Although <code class="token">COALESCE</code>, <code class="token">GREATEST</code>, and
  11. <code class="token">LEAST</code> are syntactically similar to functions, they are
  12. not ordinary functions, and thus cannot be used with explicit
  13. <code class="token">VARIADIC</code> array arguments.
  14. </p></div><div class="sect2" id="FUNCTIONS-CASE"><div class="titlepage"><div><div><h3 class="title">9.17.1. <code class="literal">CASE</code></h3></div></div></div><p>
  15. The <acronym class="acronym">SQL</acronym> <code class="token">CASE</code> expression is a
  16. generic conditional expression, similar to if/else statements in
  17. other programming languages:
  18. </p><pre class="synopsis">
  19. CASE WHEN <em class="replaceable"><code>condition</code></em> THEN <em class="replaceable"><code>result</code></em>
  20. [<span class="optional">WHEN ...</span>]
  21. [<span class="optional">ELSE <em class="replaceable"><code>result</code></em></span>]
  22. END
  23. </pre><p>
  24. <code class="token">CASE</code> clauses can be used wherever
  25. an expression is valid. Each <em class="replaceable"><code>condition</code></em> is an
  26. expression that returns a <code class="type">boolean</code> result. If the condition's
  27. result is true, the value of the <code class="token">CASE</code> expression is the
  28. <em class="replaceable"><code>result</code></em> that follows the condition, and the
  29. remainder of the <code class="token">CASE</code> expression is not processed. If the
  30. condition's result is not true, any subsequent <code class="token">WHEN</code> clauses
  31. are examined in the same manner. If no <code class="token">WHEN</code>
  32. <em class="replaceable"><code>condition</code></em> yields true, the value of the
  33. <code class="token">CASE</code> expression is the <em class="replaceable"><code>result</code></em> of the
  34. <code class="token">ELSE</code> clause. If the <code class="token">ELSE</code> clause is
  35. omitted and no condition is true, the result is null.
  36. </p><p>
  37. An example:
  38. </p><pre class="screen">
  39. SELECT * FROM test;
  40. a
  41. ---
  42. 1
  43. 2
  44. 3
  45. SELECT a,
  46. CASE WHEN a=1 THEN 'one'
  47. WHEN a=2 THEN 'two'
  48. ELSE 'other'
  49. END
  50. FROM test;
  51. a | case
  52. ---+-------
  53. 1 | one
  54. 2 | two
  55. 3 | other
  56. </pre><p>
  57. </p><p>
  58. The data types of all the <em class="replaceable"><code>result</code></em>
  59. expressions must be convertible to a single output type.
  60. See <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a> for more details.
  61. </p><p>
  62. There is a <span class="quote">“<span class="quote">simple</span>”</span> form of <code class="token">CASE</code> expression
  63. that is a variant of the general form above:
  64. </p><pre class="synopsis">
  65. CASE <em class="replaceable"><code>expression</code></em>
  66. WHEN <em class="replaceable"><code>value</code></em> THEN <em class="replaceable"><code>result</code></em>
  67. [<span class="optional">WHEN ...</span>]
  68. [<span class="optional">ELSE <em class="replaceable"><code>result</code></em></span>]
  69. END
  70. </pre><p>
  71. The first
  72. <em class="replaceable"><code>expression</code></em> is computed, then compared to
  73. each of the <em class="replaceable"><code>value</code></em> expressions in the
  74. <code class="token">WHEN</code> clauses until one is found that is equal to it. If
  75. no match is found, the <em class="replaceable"><code>result</code></em> of the
  76. <code class="token">ELSE</code> clause (or a null value) is returned. This is similar
  77. to the <code class="function">switch</code> statement in C.
  78. </p><p>
  79. The example above can be written using the simple
  80. <code class="token">CASE</code> syntax:
  81. </p><pre class="screen">
  82. SELECT a,
  83. CASE a WHEN 1 THEN 'one'
  84. WHEN 2 THEN 'two'
  85. ELSE 'other'
  86. END
  87. FROM test;
  88. a | case
  89. ---+-------
  90. 1 | one
  91. 2 | two
  92. 3 | other
  93. </pre><p>
  94. </p><p>
  95. A <code class="token">CASE</code> expression does not evaluate any subexpressions
  96. that are not needed to determine the result. For example, this is a
  97. possible way of avoiding a division-by-zero failure:
  98. </p><pre class="programlisting">
  99. SELECT ... WHERE CASE WHEN x &lt;&gt; 0 THEN y/x &gt; 1.5 ELSE false END;
  100. </pre><p>
  101. </p><div class="note"><h3 class="title">Note</h3><p>
  102. As described in <a class="xref" href="sql-expressions.html#SYNTAX-EXPRESS-EVAL" title="4.2.14. Expression Evaluation Rules">Section 4.2.14</a>, there are various
  103. situations in which subexpressions of an expression are evaluated at
  104. different times, so that the principle that <span class="quote">“<span class="quote"><code class="token">CASE</code>
  105. evaluates only necessary subexpressions</span>”</span> is not ironclad. For
  106. example a constant <code class="literal">1/0</code> subexpression will usually result in
  107. a division-by-zero failure at planning time, even if it's within
  108. a <code class="token">CASE</code> arm that would never be entered at run time.
  109. </p></div></div><div class="sect2" id="FUNCTIONS-COALESCE-NVL-IFNULL"><div class="titlepage"><div><div><h3 class="title">9.17.2. <code class="literal">COALESCE</code></h3></div></div></div><a id="id-1.5.8.22.8.2" class="indexterm"></a><a id="id-1.5.8.22.8.3" class="indexterm"></a><a id="id-1.5.8.22.8.4" class="indexterm"></a><pre class="synopsis">
  110. <code class="function">COALESCE</code>(<em class="replaceable"><code>value</code></em> [<span class="optional">, ...</span>])
  111. </pre><p>
  112. The <code class="function">COALESCE</code> function returns the first of its
  113. arguments that is not null. Null is returned only if all arguments
  114. are null. It is often used to substitute a default value for
  115. null values when data is retrieved for display, for example:
  116. </p><pre class="programlisting">
  117. SELECT COALESCE(description, short_description, '(none)') ...
  118. </pre><p>
  119. This returns <code class="varname">description</code> if it is not null, otherwise
  120. <code class="varname">short_description</code> if it is not null, otherwise <code class="literal">(none)</code>.
  121. </p><p>
  122. Like a <code class="token">CASE</code> expression, <code class="function">COALESCE</code> only
  123. evaluates the arguments that are needed to determine the result;
  124. that is, arguments to the right of the first non-null argument are
  125. not evaluated. This SQL-standard function provides capabilities similar
  126. to <code class="function">NVL</code> and <code class="function">IFNULL</code>, which are used in some other
  127. database systems.
  128. </p></div><div class="sect2" id="FUNCTIONS-NULLIF"><div class="titlepage"><div><div><h3 class="title">9.17.3. <code class="literal">NULLIF</code></h3></div></div></div><a id="id-1.5.8.22.9.2" class="indexterm"></a><pre class="synopsis">
  129. <code class="function">NULLIF</code>(<em class="replaceable"><code>value1</code></em>, <em class="replaceable"><code>value2</code></em>)
  130. </pre><p>
  131. The <code class="function">NULLIF</code> function returns a null value if
  132. <em class="replaceable"><code>value1</code></em> equals <em class="replaceable"><code>value2</code></em>;
  133. otherwise it returns <em class="replaceable"><code>value1</code></em>.
  134. This can be used to perform the inverse operation of the
  135. <code class="function">COALESCE</code> example given above:
  136. </p><pre class="programlisting">
  137. SELECT NULLIF(value, '(none)') ...
  138. </pre><p>
  139. </p><p>
  140. In this example, if <code class="literal">value</code> is <code class="literal">(none)</code>,
  141. null is returned, otherwise the value of <code class="literal">value</code>
  142. is returned.
  143. </p></div><div class="sect2" id="FUNCTIONS-GREATEST-LEAST"><div class="titlepage"><div><div><h3 class="title">9.17.4. <code class="literal">GREATEST</code> and <code class="literal">LEAST</code></h3></div></div></div><a id="id-1.5.8.22.10.2" class="indexterm"></a><a id="id-1.5.8.22.10.3" class="indexterm"></a><pre class="synopsis">
  144. <code class="function">GREATEST</code>(<em class="replaceable"><code>value</code></em> [<span class="optional">, ...</span>])
  145. </pre><pre class="synopsis">
  146. <code class="function">LEAST</code>(<em class="replaceable"><code>value</code></em> [<span class="optional">, ...</span>])
  147. </pre><p>
  148. The <code class="function">GREATEST</code> and <code class="function">LEAST</code> functions select the
  149. largest or smallest value from a list of any number of expressions.
  150. The expressions must all be convertible to a common data type, which
  151. will be the type of the result
  152. (see <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a> for details). NULL values
  153. in the list are ignored. The result will be NULL only if all the
  154. expressions evaluate to NULL.
  155. </p><p>
  156. Note that <code class="function">GREATEST</code> and <code class="function">LEAST</code> are not in
  157. the SQL standard, but are a common extension. Some other databases
  158. make them return NULL if any argument is NULL, rather than only when
  159. all are NULL.
  160. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-sequence.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-array.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.16. Sequence Manipulation Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.18. Array Functions and Operators</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1