uTools-Manuals/docs/sql/UNION 操作符.html
2019-04-21 11:50:48 +08:00

26 lines
4.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="m-bg">
<h1>SQL UNION 操作符</h1>
<p class="intro">SQL UNION 操作符合并两个或多个 SELECT 语句的结果。</p> <h2>SQL UNION 操作符</h2> <p>UNION 操作符用于合并两个或多个 SELECT 语句的结果集。</p> <p>请注意UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。</p> <h3>SQL UNION 语法</h3> <div class="code notranslate"><pre><div> SELECT <em>column_name(s)</em> FROM <em>table1</em><br/> UNION<br/> SELECT <em>column_name(s)</em> FROM <em>table2</em>;</div></pre></div> <p><b>注释:</b>默认地UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。</p> <h3>SQL UNION ALL 语法</h3> <div class="code notranslate"><pre><div> SELECT <em>column_name(s)</em> FROM <em>table1</em><br/> UNION ALL<br/> SELECT <em>column_name(s)</em> FROM <em>table2</em>;</div></pre></div> <p><b>注释:</b>UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。</p> <h2>演示数据库</h2> <p>在本教程中,我们将使用众所周知的 Northwind 样本数据库。</p> <p>下面是选自 "Customers" 表的数据:</p> <table class="reference notranslate">
<tr>
<th width="15%">CustomerID</th> <th>CustomerName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>PostalCode</th> <th>Country</th> </tr>
<tr>
<td>1<br/><br/>
</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Obere Str. 57</td> <td>Berlin</td> <td>12209</td> <td>Germany</td> </tr>
<tr>
<td>2</td> <td>Ana Trujillo Emparedados y helados</td> <td>Ana Trujillo</td> <td>Avda. de la Constitución 2222</td> <td>México D.F.</td> <td>05021</td> <td>Mexico</td> </tr>
<tr>
<td>3</td> <td>Antonio Moreno Taquería</td> <td>Antonio Moreno</td> <td>Mataderos 2312</td> <td>México D.F.</td> <td>05023</td> <td>Mexico</td> </tr>
</table>
<p>选自 "Suppliers" 表的数据:</p> <table class="reference notranslate">
<tr>
<th width="15%">SupplierID</th> <th>SupplierName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>PostalCode</th> <th>Country</th> </tr>
<tr>
<td>1</td> <td>Exotic Liquid</td> <td>Charlotte Cooper</td> <td>49 Gilbert St.</td> <td>Londona</td> <td>EC1 4SD</td> <td>UK</td> </tr>
<tr>
<td>2</td> <td>New Orleans Cajun Delights</td> <td>Shelley Burke</td> <td>P.O. Box 78934</td> <td>New Orleans</td> <td>70117</td> <td>USA</td> </tr>
<tr>
<td>3</td> <td>Grandma Kelly's Homestead</td> <td>Regina Murphy</td> <td>707 Oxford Rd.</td> <td>Ann Arbor</td> <td>48104</td> <td>USA</td> </tr>
</table>
<h2>SQL UNION 实例</h2> <p>下面的 SQL 语句从 "Customers" 和 "Suppliers" 表中选取所有<strong>不同的</strong>城市(只有不同的值):</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><div class="example_code notranslate"> SELECT City FROM Customers<br/>UNION<br/>SELECT City FROM Suppliers<br/>ORDER BY City; </div></pre> </div> <p><b>注释:</b>UNION 不能用于列出两个表中所有的城市。如果一些客户和供应商来自同一个城市每个城市只会列出一次。UNION 只会选取不同的值。请使用 UNION ALL 来选取重复的值!</p> <h2>SQL UNION ALL 实例</h2> <p>下面的 SQL 语句使用 UNION ALL 从 "Customers" 和 "Suppliers" 表中选取<strong>所有的</strong>城市(也有重复的值):</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><div class="example_code notranslate"> SELECT City FROM Customers<br/>UNION ALL<br/>SELECT City FROM Suppliers<br/> ORDER BY City; </div></pre> </div> <h2>带有 WHERE 的 SQL UNION ALL</h2> <p>下面的 SQL 语句使用 UNION ALL 从 "Customers" 和 "Suppliers" 表中选取<strong>所有的</strong>德国城市(也有重复的值):</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><div class="example_code notranslate"> SELECT City, Country FROM Customers<br/>WHERE Country='Germany'<br/>UNION ALL<br/> SELECT City, Country FROM Suppliers<br/>WHERE Country='Germany'<br/>ORDER BY City; </div></pre> </div> <div class="text-center padding-10 margin-t-5">
</div>
</div>