gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

142 linhas
7.9KB

  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>2.5. Querying a Table</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="tutorial-populate.html" title="2.4. Populating a Table With Rows" /><link rel="next" href="tutorial-join.html" title="2.6. Joins Between Tables" /></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">2.5. Querying a Table</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-populate.html" title="2.4. Populating a Table With Rows">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Up</a></td><th width="60%" align="center">Chapter 2. The <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> Language</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="tutorial-join.html" title="2.6. Joins Between Tables">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TUTORIAL-SELECT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2.5. Querying a Table</h2></div></div></div><p>
  3. <a id="id-1.4.4.6.2.1" class="indexterm"></a>
  4. <a id="id-1.4.4.6.2.2" class="indexterm"></a>
  5. To retrieve data from a table, the table is
  6. <em class="firstterm">queried</em>. An <acronym class="acronym">SQL</acronym>
  7. <code class="command">SELECT</code> statement is used to do this. The
  8. statement is divided into a select list (the part that lists the
  9. columns to be returned), a table list (the part that lists the
  10. tables from which to retrieve the data), and an optional
  11. qualification (the part that specifies any restrictions). For
  12. example, to retrieve all the rows of table
  13. <code class="structname">weather</code>, type:
  14. </p><pre class="programlisting">
  15. SELECT * FROM weather;
  16. </pre><p>
  17. Here <code class="literal">*</code> is a shorthand for <span class="quote">“<span class="quote">all columns</span>”</span>.
  18. <a href="#ftn.id-1.4.4.6.2.10" class="footnote"><sup class="footnote" id="id-1.4.4.6.2.10">[2]</sup></a>
  19. So the same result would be had with:
  20. </p><pre class="programlisting">
  21. SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
  22. </pre><p>
  23. The output should be:
  24. </p><pre class="screen">
  25. city | temp_lo | temp_hi | prcp | date
  26. ---------------+---------+---------+------+------------
  27. San Francisco | 46 | 50 | 0.25 | 1994-11-27
  28. San Francisco | 43 | 57 | 0 | 1994-11-29
  29. Hayward | 37 | 54 | | 1994-11-29
  30. (3 rows)
  31. </pre><p>
  32. </p><p>
  33. You can write expressions, not just simple column references, in the
  34. select list. For example, you can do:
  35. </p><pre class="programlisting">
  36. SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
  37. </pre><p>
  38. This should give:
  39. </p><pre class="screen">
  40. city | temp_avg | date
  41. ---------------+----------+------------
  42. San Francisco | 48 | 1994-11-27
  43. San Francisco | 50 | 1994-11-29
  44. Hayward | 45 | 1994-11-29
  45. (3 rows)
  46. </pre><p>
  47. Notice how the <code class="literal">AS</code> clause is used to relabel the
  48. output column. (The <code class="literal">AS</code> clause is optional.)
  49. </p><p>
  50. A query can be <span class="quote">“<span class="quote">qualified</span>”</span> by adding a <code class="literal">WHERE</code>
  51. clause that specifies which rows are wanted. The <code class="literal">WHERE</code>
  52. clause contains a Boolean (truth value) expression, and only rows for
  53. which the Boolean expression is true are returned. The usual
  54. Boolean operators (<code class="literal">AND</code>,
  55. <code class="literal">OR</code>, and <code class="literal">NOT</code>) are allowed in
  56. the qualification. For example, the following
  57. retrieves the weather of San Francisco on rainy days:
  58. </p><pre class="programlisting">
  59. SELECT * FROM weather
  60. WHERE city = 'San Francisco' AND prcp &gt; 0.0;
  61. </pre><p>
  62. Result:
  63. </p><pre class="screen">
  64. city | temp_lo | temp_hi | prcp | date
  65. ---------------+---------+---------+------+------------
  66. San Francisco | 46 | 50 | 0.25 | 1994-11-27
  67. (1 row)
  68. </pre><p>
  69. </p><p>
  70. <a id="id-1.4.4.6.5.1" class="indexterm"></a>
  71. You can request that the results of a query
  72. be returned in sorted order:
  73. </p><pre class="programlisting">
  74. SELECT * FROM weather
  75. ORDER BY city;
  76. </pre><p>
  77. </p><pre class="screen">
  78. city | temp_lo | temp_hi | prcp | date
  79. ---------------+---------+---------+------+------------
  80. Hayward | 37 | 54 | | 1994-11-29
  81. San Francisco | 43 | 57 | 0 | 1994-11-29
  82. San Francisco | 46 | 50 | 0.25 | 1994-11-27
  83. </pre><p>
  84. In this example, the sort order isn't fully specified, and so you
  85. might get the San Francisco rows in either order. But you'd always
  86. get the results shown above if you do:
  87. </p><pre class="programlisting">
  88. SELECT * FROM weather
  89. ORDER BY city, temp_lo;
  90. </pre><p>
  91. </p><p>
  92. <a id="id-1.4.4.6.6.1" class="indexterm"></a>
  93. <a id="id-1.4.4.6.6.2" class="indexterm"></a>
  94. You can request that duplicate rows be removed from the result of
  95. a query:
  96. </p><pre class="programlisting">
  97. SELECT DISTINCT city
  98. FROM weather;
  99. </pre><p>
  100. </p><pre class="screen">
  101. city
  102. ---------------
  103. Hayward
  104. San Francisco
  105. (2 rows)
  106. </pre><p>
  107. Here again, the result row ordering might vary.
  108. You can ensure consistent results by using <code class="literal">DISTINCT</code> and
  109. <code class="literal">ORDER BY</code> together:
  110. <a href="#ftn.id-1.4.4.6.6.7" class="footnote"><sup class="footnote" id="id-1.4.4.6.6.7">[3]</sup></a>
  111. </p><pre class="programlisting">
  112. SELECT DISTINCT city
  113. FROM weather
  114. ORDER BY city;
  115. </pre><p>
  116. </p><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.4.4.6.2.10" class="footnote"><p><a href="#id-1.4.4.6.2.10" class="para"><sup class="para">[2] </sup></a>
  117. While <code class="literal">SELECT *</code> is useful for off-the-cuff
  118. queries, it is widely considered bad style in production code,
  119. since adding a column to the table would change the results.
  120. </p></div><div id="ftn.id-1.4.4.6.6.7" class="footnote"><p><a href="#id-1.4.4.6.6.7" class="para"><sup class="para">[3] </sup></a>
  121. In some database systems, including older versions of
  122. <span class="productname">PostgreSQL</span>, the implementation of
  123. <code class="literal">DISTINCT</code> automatically orders the rows and
  124. so <code class="literal">ORDER BY</code> is unnecessary. But this is not
  125. required by the SQL standard, and current
  126. <span class="productname">PostgreSQL</span> does not guarantee that
  127. <code class="literal">DISTINCT</code> causes the rows to be ordered.
  128. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-populate.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-sql.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-join.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.4. Populating a Table With Rows </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 2.6. Joins Between Tables</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1