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.

162 lines
9.6KB

  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.6. Joins Between Tables</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-select.html" title="2.5. Querying a Table" /><link rel="next" href="tutorial-agg.html" title="2.7. Aggregate 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">2.6. Joins Between Tables</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-select.html" title="2.5. Querying a Table">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-agg.html" title="2.7. Aggregate Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TUTORIAL-JOIN"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2.6. Joins Between Tables</h2></div></div></div><a id="id-1.4.4.7.2" class="indexterm"></a><p>
  3. Thus far, our queries have only accessed one table at a time.
  4. Queries can access multiple tables at once, or access the same
  5. table in such a way that multiple rows of the table are being
  6. processed at the same time. A query that accesses multiple rows
  7. of the same or different tables at one time is called a
  8. <em class="firstterm">join</em> query. As an example, say you wish to
  9. list all the weather records together with the location of the
  10. associated city. To do that, we need to compare the <code class="structfield">city</code>
  11. column of each row of the <code class="structname">weather</code> table with the
  12. <code class="structfield">name</code> column of all rows in the <code class="structname">cities</code>
  13. table, and select the pairs of rows where these values match.
  14. </p><div class="note"><h3 class="title">Note</h3><p>
  15. This is only a conceptual model. The join is usually performed
  16. in a more efficient manner than actually comparing each possible
  17. pair of rows, but this is invisible to the user.
  18. </p></div><p>
  19. This would be accomplished by the following query:
  20. </p><pre class="programlisting">
  21. SELECT *
  22. FROM weather, cities
  23. WHERE city = name;
  24. </pre><p>
  25. </p><pre class="screen">
  26. city | temp_lo | temp_hi | prcp | date | name | location
  27. ---------------+---------+---------+------+------------+---------------+-----------
  28. San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
  29. San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
  30. (2 rows)
  31. </pre><p>
  32. </p><p>
  33. Observe two things about the result set:
  34. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  35. There is no result row for the city of Hayward. This is
  36. because there is no matching entry in the
  37. <code class="structname">cities</code> table for Hayward, so the join
  38. ignores the unmatched rows in the <code class="structname">weather</code> table. We will see
  39. shortly how this can be fixed.
  40. </p></li><li class="listitem"><p>
  41. There are two columns containing the city name. This is
  42. correct because the lists of columns from the
  43. <code class="structname">weather</code> and
  44. <code class="structname">cities</code> tables are concatenated. In
  45. practice this is undesirable, though, so you will probably want
  46. to list the output columns explicitly rather than using
  47. <code class="literal">*</code>:
  48. </p><pre class="programlisting">
  49. SELECT city, temp_lo, temp_hi, prcp, date, location
  50. FROM weather, cities
  51. WHERE city = name;
  52. </pre><p>
  53. </p></li></ul></div><p>
  54. </p><p><strong>Exercise: </strong>
  55. Attempt to determine the semantics of this query when the
  56. <code class="literal">WHERE</code> clause is omitted.
  57. </p><p>
  58. Since the columns all had different names, the parser
  59. automatically found which table they belong to. If there
  60. were duplicate column names in the two tables you'd need to
  61. <em class="firstterm">qualify</em> the column names to show which one you
  62. meant, as in:
  63. </p><pre class="programlisting">
  64. SELECT weather.city, weather.temp_lo, weather.temp_hi,
  65. weather.prcp, weather.date, cities.location
  66. FROM weather, cities
  67. WHERE cities.name = weather.city;
  68. </pre><p>
  69. It is widely considered good style to qualify all column names
  70. in a join query, so that the query won't fail if a duplicate
  71. column name is later added to one of the tables.
  72. </p><p>
  73. Join queries of the kind seen thus far can also be written in this
  74. alternative form:
  75. </p><pre class="programlisting">
  76. SELECT *
  77. FROM weather INNER JOIN cities ON (weather.city = cities.name);
  78. </pre><p>
  79. This syntax is not as commonly used as the one above, but we show
  80. it here to help you understand the following topics.
  81. </p><p>
  82. <a id="id-1.4.4.7.8.1" class="indexterm"></a>
  83. Now we will figure out how we can get the Hayward records back in.
  84. What we want the query to do is to scan the
  85. <code class="structname">weather</code> table and for each row to find the
  86. matching <code class="structname">cities</code> row(s). If no matching row is
  87. found we want some <span class="quote">“<span class="quote">empty values</span>”</span> to be substituted
  88. for the <code class="structname">cities</code> table's columns. This kind
  89. of query is called an <em class="firstterm">outer join</em>. (The
  90. joins we have seen so far are inner joins.) The command looks
  91. like this:
  92. </p><pre class="programlisting">
  93. SELECT *
  94. FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
  95. city | temp_lo | temp_hi | prcp | date | name | location
  96. ---------------+---------+---------+------+------------+---------------+-----------
  97. Hayward | 37 | 54 | | 1994-11-29 | |
  98. San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53)
  99. San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53)
  100. (3 rows)
  101. </pre><p>
  102. This query is called a <em class="firstterm">left outer
  103. join</em> because the table mentioned on the left of the
  104. join operator will have each of its rows in the output at least
  105. once, whereas the table on the right will only have those rows
  106. output that match some row of the left table. When outputting a
  107. left-table row for which there is no right-table match, empty (null)
  108. values are substituted for the right-table columns.
  109. </p><p><strong>Exercise: </strong>
  110. There are also right outer joins and full outer joins. Try to
  111. find out what those do.
  112. </p><p>
  113. <a id="id-1.4.4.7.10.1" class="indexterm"></a>
  114. <a id="id-1.4.4.7.10.2" class="indexterm"></a>
  115. We can also join a table against itself. This is called a
  116. <em class="firstterm">self join</em>. As an example, suppose we wish
  117. to find all the weather records that are in the temperature range
  118. of other weather records. So we need to compare the
  119. <code class="structfield">temp_lo</code> and <code class="structfield">temp_hi</code> columns of
  120. each <code class="structname">weather</code> row to the
  121. <code class="structfield">temp_lo</code> and
  122. <code class="structfield">temp_hi</code> columns of all other
  123. <code class="structname">weather</code> rows. We can do this with the
  124. following query:
  125. </p><pre class="programlisting">
  126. SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,
  127. W2.city, W2.temp_lo AS low, W2.temp_hi AS high
  128. FROM weather W1, weather W2
  129. WHERE W1.temp_lo &lt; W2.temp_lo
  130. AND W1.temp_hi &gt; W2.temp_hi;
  131. city | low | high | city | low | high
  132. ---------------+-----+------+---------------+-----+------
  133. San Francisco | 43 | 57 | San Francisco | 46 | 50
  134. Hayward | 37 | 54 | San Francisco | 46 | 50
  135. (2 rows)
  136. </pre><p>
  137. Here we have relabeled the weather table as <code class="literal">W1</code> and
  138. <code class="literal">W2</code> to be able to distinguish the left and right side
  139. of the join. You can also use these kinds of aliases in other
  140. queries to save some typing, e.g.:
  141. </p><pre class="programlisting">
  142. SELECT *
  143. FROM weather w, cities c
  144. WHERE w.city = c.name;
  145. </pre><p>
  146. You will encounter this style of abbreviating quite frequently.
  147. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-select.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-agg.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.5. Querying a Table </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 2.7. Aggregate Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1