uTools-Manuals/docs/sql/COUNT() 函数.html
2019-05-07 10:15:08 +08:00

15 lines
2.6 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 COUNT() 函数</h1>
<p class="intro">COUNT() 函数返回匹配指定条件的行数。</p> <h3>SQL COUNT(column_name) 语法</h3> <p>COUNT(column_name) 函数返回指定列的值的数目NULL 不计入):</p> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT COUNT(column_name) FROM table_name;</div></code></pre></div> <h3>SQL COUNT(*) 语法</h3> <p>COUNT(*) 函数返回表中的记录数:</p> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT COUNT(*) FROM table_name;</div></code></pre></div> <h3>SQL COUNT(DISTINCT column_name) 语法</h3> <p>COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:</p> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT COUNT(DISTINCT column_name) FROM table_name;</div></code></pre></div> <p><b>注释:</b>COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server但是无法用于 Microsoft Access。</p> <h2>演示数据库</h2> <p>在本教程中,我们将使用众所周知的 Northwind 样本数据库。</p> <p>下面是选自 "Orders" 表的数据:</p> <table class="reference notranslate">
<tr>
<th>OrderID</th> <th>CustomerID</th> <th>EmployeeID</th> <th>OrderDate</th> <th>ShipperID</th> </tr>
<tr>
<td>10265</td> <td>7</td> <td>2</td> <td>1996-07-25</td> <td>1</td> </tr>
<tr>
<td>10266</td> <td>87</td> <td>3</td> <td>1996-07-26</td> <td>3</td> </tr>
<tr>
<td>10267</td> <td>25</td> <td>4</td> <td>1996-07-29</td> <td>1</td> </tr>
</table>
<h2>SQL COUNT(column_name) 实例</h2> <p>下面的 SQL 语句计算 "Orders" 表中 "CustomerID"=7 的订单数:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM Orders<br/>WHERE CustomerID=7; </div></code></pre> </div> <h2>SQL COUNT(*) 实例</h2> <p>下面的 SQL 语句计算 "Orders" 表中的订单总数:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT COUNT(*) AS NumberOfOrders FROM Orders; </div></code></pre> </div> <h2>SQL COUNT(DISTINCT column_name) 实例</h2> <p>下面的 SQL 语句计算 "Orders" 表中不同客户的数目:</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers FROM Orders; </div></code></pre> </div> <br/><br/><div class="text-center padding-10 margin-t-5">
</div>
</div>