uTools-Manuals/docs/sql/通配符.html
2019-05-07 10:15:08 +08:00

33 lines
4.2 KiB
HTML

<div class="m-bg">
<h1>SQL 通配符 </h1>
<p class="intro">通配符可用于替代字符串中的任何其他字符。</p> <h2>SQL 通配符</h2> <p>在 SQL 中,通配符与 SQL LIKE 操作符一起使用。</p> <p>SQL 通配符用于搜索表中的数据。</p> <p>在 SQL 中,可使用一下通配符:</p> <table class="reference notranslate">
<tr>
<th width="15%">通配符</th> <th width="85%">描述</th> </tr>
<tr>
<td>%</td> <td>替代 0 个或多个字符</td> </tr>
<tr>
<td>_</td> <td>替代一个字符</td> </tr>
<tr>
<td>[<em>charlist</em>]</td> <td>字符列中的任何单一字符</td> </tr>
<tr>
<td>[^<em>charlist</em>]<br/>or<br/>[!<em>charlist</em>]</td> <td>不在字符列中的任何单一字符</td> </tr>
</table>
<h2>演示数据库</h2> <p>在本教程中,我们将使用众所周知的 Northwind 样本数据库。</p> <p>下面是选自 "Customers" 表的数据:</p> <table class="reference notranslate">
<tr>
<th>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>
<tr>
<td>4<br/><br/>
</td> <td>Around the Horn</td> <td>Thomas Hardy</td> <td>120 Hanover Sq.</td> <td>London</td> <td>WA1 1DP</td> <td>UK</td> </tr>
<tr>
<td>5</td> <td>Berglunds snabbköp</td> <td>Christina Berglund</td> <td>Berguvsvägen 8</td> <td>Luleå</td> <td>S-958 22</td> <td>Sweden</td> </tr>
</table>
<h2>使用 SQL % 通配符</h2> <p>下面的 SQL 语句选取 City 以字母 "ber" 开始的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE 'ber%'; </div></code></pre> </div> <p>下面的 SQL 语句选取 City 包含模式 "es" 的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE '%es%'; </div></code></pre> </div> <h2>使用 SQL _ 通配符</h2> <p>下面的 SQL 语句选取 City 以一个任意字符开始,然后是 "erlin" 的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE '_erlin'; </div></code></pre> </div> <p>下面的 SQL 语句选取 City 以 "L" 开始,然后是一个任意字符,然后是 "n",然后是一个任意字符,然后是 "on" 的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE 'L_n_on'; </div></code></pre> </div> <h2>使用 SQL [charlist] 通配符</h2> <p>下面的 SQL 语句选取 City 以 "b"、"s" 或 "p" 开始的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE '[bsp]%'; </div></code></pre> </div> <p>下面的 SQL 语句选取 City 以 "a"、"b" 或 "c" 开始的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE '[a-c]%'; </div></code></pre> </div> <p>下面的 SQL 语句选取 City 不以 "b"、"s" 或 "p" 开始的所有客户:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE City LIKE '[!bsp]%'; </div></code></pre> </div> <div class="text-center padding-10 margin-t-5">
</div>
</div>