uTools-Manuals/docs/sql/INSERT INTO SELECT 语句.html
2019-05-07 10:15:08 +08:00

26 lines
3.4 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 INSERT INTO SELECT 语句</h1>
<p class="intro">通过 SQL您可以从一个表复制信息到另一个表。</p> <p class="intro">INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。</p> <h2>SQL INSERT INTO SELECT 语句</h2> <p>INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。</p> <h3>SQL INSERT INTO SELECT 语法</h3> <p>我们可以从一个表中复制所有的列插入到另一个已存在的表中:</p> <div class="code notranslate"><pre><code class="language-sql"><div> INSERT INTO <em>table2</em><br/> SELECT * FROM <em>table1;</em> </div></code></pre></div> <p>或者我们可以只复制希望的列插入到另一个已存在的表中:</p> <div class="code notranslate"><pre><code class="language-sql"><div> INSERT INTO <em>table2</em><br/><em>(column_name(s))</em><br/> SELECT <em>column_name(s)</em><br/> FROM <em>table1;</em> </div></code></pre></div> <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>SupplierID</th> <th>SupplierName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>Postal Code</th> <th>Country</th> <th>Phone</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> <td>(171) 555-2222</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> <td>(100) 555-4822</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> <td>(313) 555-5735</td> </tr>
</table>
<h2>SQL INSERT INTO SELECT 实例</h2> <p>只复制 "Suppliers" 中的一些列插入到 "Customers" 中:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> INSERT INTO Customers (CustomerName, Country)<br/> SELECT SupplierName, Country FROM Suppliers; </div></code></pre> </div> <p>只复制德国的供应商插入到 "Customers" 中:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> INSERT INTO Customers (CustomerName, Country)<br/> SELECT SupplierName, Country FROM Suppliers<br/>WHERE Country='Germany'; </div></code></pre> </div> <div class="text-center padding-10 margin-t-5">
</div>
</div>