本文共 3828 字,大约阅读时间需要 12 分钟。
sql顶部菜单项消失
SQL SELECT TOP clause is very useful when dealing with a huge set of data or tables with a huge amount of data.
当处理大量数据或包含大量数据的表时,SQL SELECT TOP子句非常有用。
This clause is used to restrict the number of records to return a result set.
此子句用于限制返回结果集的记录数。
The SELECT TOP clause can be used in two ways.
SELECT TOP子句可以两种方式使用。
We will try to understand both the usage in detail in below-mentioned sections.
我们将在以下各节中尝试详细了解这两种用法。
We will try to understand the syntax for using SQL SELECT TOP by providing the number of records to return as the result set.
通过提供要返回的记录数作为结果集,我们将尝试了解使用SQL SELECT TOP的语法。
Select Top Syntax
选择顶部语法
SELECT TOP number column_name FROM table_name WHERE condition;
In the syntax above all the columns data is retrieved based on the WHERE clause and restricted by the number that is provided as part of the SELECT TOP.
在以上语法中,所有列数据都是基于WHERE子句检索的,并受作为SELECT TOP一部分提供的数字的限制。
Let’s consider the following Customer Table to understand SELECT TOP command for copying all the columns data based on a condition.
让我们考虑下面的客户表,以了解用于根据条件复制所有列数据的SELECT TOP命令。
Customer:
顾客:
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
1 | John | 31 | M |
2 | Amit | 25 | M |
3 | Annie | 35 | F |
4 | Tom | 38 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
1个 | 约翰 | 31 | 中号 |
2 | 阿米特 | 25 | 中号 |
3 | 安妮 | 35 | F |
4 | 汤姆 | 38 | 中号 |
Scenario:
场景 :
Get the first-row data from Customer table where gender is male.
从“客户”表中获取性别为男性的第一行数据。
SELECT TOP 1 * FROM Customer WHERE CustomerGender = 'M';
Output:
输出 :
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
1 | John | 31 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
1个 | 约翰 | 31 | 中号 |
We will try to understand the syntax for using SQL SELECT TOP by providing the percentage of records to return as the result set.
通过提供要返回的记录百分比作为结果集,我们将尝试了解使用SQL SELECT TOP的语法。
Syntax:
语法 :
SELECT TOP number PERCENT column_name FROM table_name WHERE condition;
In the syntax above all the columns data is retrieved based on the WHERE clause and restricted by the percent that is provided as part of the SELECT TOP.
在以上语法中,所有列数据都是基于WHERE子句检索的,并受作为SELECT TOP一部分提供的百分比的限制。
Let’s consider the following Customer Table to understand SELECT INTO command for copying all the columns data based on a condition.
让我们考虑以下客户表,以了解用于根据条件复制所有列数据的SELECT INTO命令。
Scenario:
场景 :
Get 50% rows data from Customer table where gender is male.
从“客户”表中获取性别为男性的50%行数据。
Query:
查询 :
SELECT TOP 50 PERCENT * FROM Customer WHERE CustomerGender = 'M';
Output:
输出 :
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
1 | John | 31 | M |
2 | Amit | 25 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
1个 | 约翰 | 31 | 中号 |
2 | 阿米特 | 25 | 中号 |
We can combine multiple SELECT TOP statements to get the desired result
我们可以组合多个SELECT TOP语句以获得所需的结果
Syntax:
语法 :
SELECT TOP number column_name FROM table_name WHERE condition (the select statement with another SELECT TOP);
Scenario:
场景 :
Get the first-row data from Customer table where gender is male.
从“客户”表中获取性别为男性的第一行数据。
Query:
查询 :
SELECT TOP 1 * FROM Customer WHERE CustomerAge = (SELECT TOP 1 CustomerAge FROM Customer ORDER BY CustomerAge desc);
Output:
输出 :
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
4 | Tom | 38 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
4 | 汤姆 | 38 | 中号 |
翻译自:
sql顶部菜单项消失
转载地址:http://tlmzd.baihongyu.com/