gooderp18绿色标准版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

76 lines
7.1KB

  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.5. Sorting Rows</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-union.html" title="7.4. Combining Queries" /><link rel="next" href="queries-limit.html" title="7.6. LIMIT and OFFSET" /></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.5. Sorting Rows</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-union.html" title="7.4. Combining Queries">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-limit.html" title="7.6. LIMIT and OFFSET">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="QUERIES-ORDER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.5. Sorting Rows</h2></div></div></div><a id="id-1.5.6.9.2" class="indexterm"></a><a id="id-1.5.6.9.3" class="indexterm"></a><p>
  3. After a query has produced an output table (after the select list
  4. has been processed) it can optionally be sorted. If sorting is not
  5. chosen, the rows will be returned in an unspecified order. The actual
  6. order in that case will depend on the scan and join plan types and
  7. the order on disk, but it must not be relied on. A particular
  8. output ordering can only be guaranteed if the sort step is explicitly
  9. chosen.
  10. </p><p>
  11. The <code class="literal">ORDER BY</code> clause specifies the sort order:
  12. </p><pre class="synopsis">
  13. SELECT <em class="replaceable"><code>select_list</code></em>
  14. FROM <em class="replaceable"><code>table_expression</code></em>
  15. ORDER BY <em class="replaceable"><code>sort_expression1</code></em> [<span class="optional">ASC | DESC</span>] [<span class="optional">NULLS { FIRST | LAST }</span>]
  16. [<span class="optional">, <em class="replaceable"><code>sort_expression2</code></em> [<span class="optional">ASC | DESC</span>] [<span class="optional">NULLS { FIRST | LAST }</span>] ...</span>]
  17. </pre><p>
  18. The sort expression(s) can be any expression that would be valid in the
  19. query's select list. An example is:
  20. </p><pre class="programlisting">
  21. SELECT a, b FROM table1 ORDER BY a + b, c;
  22. </pre><p>
  23. When more than one expression is specified,
  24. the later values are used to sort rows that are equal according to the
  25. earlier values. Each expression can be followed by an optional
  26. <code class="literal">ASC</code> or <code class="literal">DESC</code> keyword to set the sort direction to
  27. ascending or descending. <code class="literal">ASC</code> order is the default.
  28. Ascending order puts smaller values first, where
  29. <span class="quote">“<span class="quote">smaller</span>”</span> is defined in terms of the
  30. <code class="literal">&lt;</code> operator. Similarly, descending order is
  31. determined with the <code class="literal">&gt;</code> operator.
  32. <a href="#ftn.id-1.5.6.9.5.10" class="footnote"><sup class="footnote" id="id-1.5.6.9.5.10">[5]</sup></a>
  33. </p><p>
  34. The <code class="literal">NULLS FIRST</code> and <code class="literal">NULLS LAST</code> options can be
  35. used to determine whether nulls appear before or after non-null values
  36. in the sort ordering. By default, null values sort as if larger than any
  37. non-null value; that is, <code class="literal">NULLS FIRST</code> is the default for
  38. <code class="literal">DESC</code> order, and <code class="literal">NULLS LAST</code> otherwise.
  39. </p><p>
  40. Note that the ordering options are considered independently for each
  41. sort column. For example <code class="literal">ORDER BY x, y DESC</code> means
  42. <code class="literal">ORDER BY x ASC, y DESC</code>, which is not the same as
  43. <code class="literal">ORDER BY x DESC, y DESC</code>.
  44. </p><p>
  45. A <em class="replaceable"><code>sort_expression</code></em> can also be the column label or number
  46. of an output column, as in:
  47. </p><pre class="programlisting">
  48. SELECT a + b AS sum, c FROM table1 ORDER BY sum;
  49. SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
  50. </pre><p>
  51. both of which sort by the first output column. Note that an output
  52. column name has to stand alone, that is, it cannot be used in an expression
  53. — for example, this is <span class="emphasis"><em>not</em></span> correct:
  54. </p><pre class="programlisting">
  55. SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong
  56. </pre><p>
  57. This restriction is made to reduce ambiguity. There is still
  58. ambiguity if an <code class="literal">ORDER BY</code> item is a simple name that
  59. could match either an output column name or a column from the table
  60. expression. The output column is used in such cases. This would
  61. only cause confusion if you use <code class="literal">AS</code> to rename an output
  62. column to match some other table column's name.
  63. </p><p>
  64. <code class="literal">ORDER BY</code> can be applied to the result of a
  65. <code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, or <code class="literal">EXCEPT</code>
  66. combination, but in this case it is only permitted to sort by
  67. output column names or numbers, not by expressions.
  68. </p><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.5.6.9.5.10" class="footnote"><p><a href="#id-1.5.6.9.5.10" class="para"><sup class="para">[5] </sup></a>
  69. Actually, <span class="productname">PostgreSQL</span> uses the <em class="firstterm">default B-tree
  70. operator class</em> for the expression's data type to determine the sort
  71. ordering for <code class="literal">ASC</code> and <code class="literal">DESC</code>. Conventionally,
  72. data types will be set up so that the <code class="literal">&lt;</code> and
  73. <code class="literal">&gt;</code> operators correspond to this sort ordering,
  74. but a user-defined data type's designer could choose to do something
  75. different.
  76. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-union.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-limit.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.4. Combining Queries </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 7.6. <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1