#**SQL 关键字 ORDER BY**
---
ORDER BY 关键字用于将查询到的结果集排序。
---
###**SQL 关键字 ORDER BY**
ORDER BY 关键字用于将查询到的一个字段或多个字段的结果集排序。
ORDER BY 默认将结果集升序排序。若要实现降序排序,可以使用关键字 DESC 。
###**SQL ORDER BY 的语法**
> SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC | DESC, column_name ASC | DESC;
---
###**数据表示例**
|**CustomerID**|**CustomerName**|**ContactName**|**Address**|**City**|**PostalCode**|**Country**|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|1|Alfreds Futterkiste|Maria Anders|Obere Str. 57|Berlin|12209|Germany|
|2|Ana Trujillo Emparedados y helados|Ana Trujillo|Avda. de la Constitución 2222|México D.F.|05021|Mexico|
|3|Antonio Moreno Taquería|Antonio Moreno|Mataderos 2312|México D.F.|05023|Mexico|
|4|Around the Horn|Thomas Hardy|120 Hanover Sq.|London|WA1 1DP|UK|
|5|Berglunds snabbköp|Christina Berglund|Berguvsvägen 8|Luleå|S-958 22|Sweden|
---
###**ORDER BY 示例**
如下SQL语句查询 Customers 表的所有用户按照 Country排序(默认正序排序)
> Example
`SELECT * FROM Customers
ORDER BY Country;`
[亲自试一试 »](http://w3schools.bootcss.com/sql/trysql_select_orderby.html?filename=trysql_select_orderby)
---
###**ORDER BY DESC 示例**
如下SQL语句查询 Customers 表的所有用户按照 Country 倒序排序(DESC)
> Example
`SELECT * FROM Customers
ORDER BY Country DESC;`
[亲自试一试 »](http://w3schools.bootcss.com/sql/trysql_select_orderby_desc.html?filename=trysql_select_orderby_desc)
---
###**多个字段 ORDER BY 排序示例1**
如下SQL语句查询 Customers 表的所有用户按照 Country 字段和 CustomerName 正序排序
(译者注:这里是先按 Country 正序排序再按 CustomerName 正序排序)
> Example
`SELECT * FROM Customers
ORDER BY Country, CustomerName;`
[亲自试一试 »](http://w3schools.bootcss.com/sql/trysql_select_orderby2.html?filename=trysql_select_orderby2)
---
###**多个字段 ORDER BY 排序示例2**
如下SQL语句查询 Customers 表的所有用户按照 Country 字段 正序排序再按照 CustomerName 倒序排序
> Example
`SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;`
[亲自试一试 »](http://w3schools.bootcss.com/sql/trysql_select_orderby3.html?filename=trysql_select_orderby3)
<br>
<br>
<br>
<br>
<br>
- sql文档
- sql简介
- sql语法
- select
- distinct
- where
- and & or
- order by
- insert into
- update
- delete
- injection
- select top
- like
- wildcards
- in
- between
- aliases
- joins
- inner join
- left join
- union
- select into
- insert into select
- create db
- create table
- constraints
- not null
- unique
- primary key
- foreign key
- check
- sql default
- create index
- create index
- drop
- alter
- auto increament
- views
- dates
- null values
- null functions
- data types
- db data types
- SQL Functions
- avg
- count
- first
- last
- max
- min
- sum
- group by
- having
- mid
- len
- round
- now
- format
- quick ref
- hosting
- sql quiz