sql limit 子句
SQL排序依据 (SQL Order By)
- SQL Order By clause is used for sorting the result set. SQL Order By子句用于对结果集进行排序。
- The sorting is done in either ascending or in descending order. 排序以升序或降序进行。
- The default order of sorting is sorting in ascending order. 默认的排序顺序是升序排序。
- SQL Order By clause is used with SQL Select queries. Sorting is done after the result set is obtained SQL Order By子句与SQL Select查询一起使用。 获得结果集后进行排序
- We can use multiple columns with order by clause, sorting will happen from left side columns first and then towards the left side columns. We will look at sql order by example with multiple columns to understand this scenario. 我们可以在order by子句中使用多个列,排序将首先从左侧的列开始,然后再朝左侧的列进行。 我们将通过带有多个列的示例来查看sql order以了解这种情况。
SQL语法排序 (SQL Order By Syntax)
Let’s now try to understand syntax for SQL ORDER BY clause. Below is the syntax for getting results in ascending order.
现在,让我们尝试了解SQL ORDER BY子句的语法。 以下是按升序获取结果的语法。
SELECT column FROM table_name ORDER BY column asc;
In the syntax above the column, data is retrieved in ascending order.
在该列上方的语法中,数据以升序检索。
Let’s try to understand the ORDER BY command for ascending order through some example.
让我们尝试通过一些示例来理解ORDER BY命令的升序。
Let us consider the following Customer Table to understand ORDER BY command.
让我们考虑以下客户表,以了解ORDER BY命令。
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
1 | John | 31 | M |
2 | Amit | 25 | M |
3 | Annie | 35 | F |
4 | null | 38 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
1个 | 约翰 | 31 | 中号 |
2 | 阿米特 | 25 | 中号 |
3 | 安妮 | 35 | F |
4 | 空值 | 38 | 中号 |
Scenario: Retrieve name of the customers in ascending order.
场景 :按升序检索客户名称。
Query:
查询 :
SELECT CutomerName FROM Customer ORDER BY CustomerName asc;
CustomerName |
---|
null |
Amit |
Annie |
John |
顾客姓名 |
---|
空值 |
阿米特 |
安妮 |
约翰 |
SQL按多列排序 (SQL Order By Multiple Columns)
We can sort the data based on multiple columns also. Let’s consider the following scenario to understand multiple column sorting.
我们还可以基于多个列对数据进行排序。 让我们考虑以下情形,以了解多列排序。
Scenario: Retrieve customer data based on CustomerGender and CustomerName in ascending order.
场景 :基于CustomerGender和CustomerName升序检索客户数据。
Query:
查询:
SELECT * FROM Customer ORDER BY CustomerGender, CustomerName ASC;
Output:
输出 :
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
3 | Annie | 35 | F |
4 | null | 38 | M |
2 | Amit | 25 | M |
1 | John | 31 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
3 | 安妮 | 35 | F |
4 | 空值 | 38 | 中号 |
2 | 阿米特 | 25 | 中号 |
1个 | 约翰 | 31 | 中号 |
Notice that first sorting is done on gender and then on the name. By default “null” values comes first in ascending order values.
注意,首先对性别进行排序,然后对名称进行排序。 默认情况下,“ null”值在升序值中排在首位。
SQL降序排列 (SQL Order By Descending)
SELECT column FROM table_name ORDER BY column desc;
In the syntax above the column, data is retrieved in descending order.
在该列上方的语法中,数据以降序检索。
Let’s try to understand the ORDER BY command for descending through some example. We will use the Customer table provided above for our example.
让我们尝试了解ORDER BY命令,以了解一些示例。 我们将使用上面提供的Customer表作为示例。
Scenario: Retrieve name of the customers in descending order.
场景 :按降序检索客户名称。
Query:
查询 :
SELECT CutomerName FROM Customer ORDER BY CustomerName desc;
Output:
输出:
CustomerName |
---|
John |
Annie |
Amit |
null |
顾客姓名 |
---|
约翰 |
安妮 |
阿米特 |
空值 |
SQL顺序按多个升序降序 (SQL Order By Multiple Ascending Descending)
Scenario: Retrieve customer data based on CustomerGender in ascending and CustomerName in descending order.
场景 :基于CustomerGender升序和CustomerName降序检索客户数据。
Query:
查询 :
SELECT * FROM Customer ORDER BY CustomerGender ASC, CustomerName desc;
Output:
输出:
CustomerId | CustomerName | CustomerAge | CustomerGender |
---|---|---|---|
3 | Annie | 35 | F |
1 | John | 31 | M |
2 | Amit | 25 | M |
4 | null | 38 | M |
顾客ID | 顾客姓名 | 客户年龄 | 客户性别 |
---|---|---|---|
3 | 安妮 | 35 | F |
1个 | 约翰 | 31 | 中号 |
2 | 阿米特 | 25 | 中号 |
4 | 空值 | 38 | 中号 |
That’s all for a quick roundup on SQL Order By clause.
这就是对SQL Order By子句进行快速汇总的全部内容。
Reference: Oracle Documentation
参考: Oracle文档
翻译自: https://www.journaldev.com/18675/sql-order-by-clause
sql limit 子句
最后
以上就是过时飞鸟最近收集整理的关于sql limit 子句_SQL按子句排序的全部内容,更多相关sql内容请搜索靠谱客的其他文章。
发表评论 取消回复