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.

45 line
5.3KB

  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>7.4. Combining Queries</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="queries-select-lists.html" title="7.3. Select Lists" /><link rel="next" href="queries-order.html" title="7.5. Sorting Rows" /></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">7.4. Combining Queries</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-select-lists.html" title="7.3. Select Lists">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><th width="60%" align="center">Chapter 7. Queries</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="queries-order.html" title="7.5. Sorting Rows">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="QUERIES-UNION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.4. Combining Queries</h2></div></div></div><a id="id-1.5.6.8.2" class="indexterm"></a><a id="id-1.5.6.8.3" class="indexterm"></a><a id="id-1.5.6.8.4" class="indexterm"></a><a id="id-1.5.6.8.5" class="indexterm"></a><a id="id-1.5.6.8.6" class="indexterm"></a><a id="id-1.5.6.8.7" class="indexterm"></a><a id="id-1.5.6.8.8" class="indexterm"></a><p>
  3. The results of two queries can be combined using the set operations
  4. union, intersection, and difference. The syntax is
  5. </p><pre class="synopsis">
  6. <em class="replaceable"><code>query1</code></em> UNION [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em>
  7. <em class="replaceable"><code>query1</code></em> INTERSECT [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em>
  8. <em class="replaceable"><code>query1</code></em> EXCEPT [<span class="optional">ALL</span>] <em class="replaceable"><code>query2</code></em>
  9. </pre><p>
  10. <em class="replaceable"><code>query1</code></em> and
  11. <em class="replaceable"><code>query2</code></em> are queries that can use any of
  12. the features discussed up to this point. Set operations can also
  13. be nested and chained, for example
  14. </p><pre class="synopsis">
  15. <em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em> UNION <em class="replaceable"><code>query3</code></em>
  16. </pre><p>
  17. which is executed as:
  18. </p><pre class="synopsis">
  19. (<em class="replaceable"><code>query1</code></em> UNION <em class="replaceable"><code>query2</code></em>) UNION <em class="replaceable"><code>query3</code></em>
  20. </pre><p>
  21. </p><p>
  22. <code class="literal">UNION</code> effectively appends the result of
  23. <em class="replaceable"><code>query2</code></em> to the result of
  24. <em class="replaceable"><code>query1</code></em> (although there is no guarantee
  25. that this is the order in which the rows are actually returned).
  26. Furthermore, it eliminates duplicate rows from its result, in the same
  27. way as <code class="literal">DISTINCT</code>, unless <code class="literal">UNION ALL</code> is used.
  28. </p><p>
  29. <code class="literal">INTERSECT</code> returns all rows that are both in the result
  30. of <em class="replaceable"><code>query1</code></em> and in the result of
  31. <em class="replaceable"><code>query2</code></em>. Duplicate rows are eliminated
  32. unless <code class="literal">INTERSECT ALL</code> is used.
  33. </p><p>
  34. <code class="literal">EXCEPT</code> returns all rows that are in the result of
  35. <em class="replaceable"><code>query1</code></em> but not in the result of
  36. <em class="replaceable"><code>query2</code></em>. (This is sometimes called the
  37. <em class="firstterm">difference</em> between two queries.) Again, duplicates
  38. are eliminated unless <code class="literal">EXCEPT ALL</code> is used.
  39. </p><p>
  40. In order to calculate the union, intersection, or difference of two
  41. queries, the two queries must be <span class="quote">“<span class="quote">union compatible</span>”</span>,
  42. which means that they return the same number of columns and
  43. the corresponding columns have compatible data types, as
  44. described in <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>.
  45. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-select-lists.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-order.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.3. Select Lists </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 7.5. Sorting Rows</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1