SQL ORDER BY 关键字

ORDER BY 关键字用于对结果集按照一个列或者多个列进行排序。

ORDER BY 关键字默认按照升序对记录进行排序。如果需要按照降序对记录进行排序,您可以使用 DESC 关键字。

 

1. ORDER BY 语法

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

其中ASC为升序,DESC为降序。如果不指定ASC或者DESC,那么默认为升序排列。


样本数据库

在本教程中,我们将使用 CodeBaoku 样本数据库。

下面是选自 "Websites" 表的数据:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 编程宝库      | http://www.codebaoku.com/ | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

 

2. ORDER BY ASC 范例

下面的 SQL 语句从 "Websites" 表中选取所有网站,并按照 "alexa" 列升序排序:

SELECT * FROM Websites ORDER BY alexa ASC; 

或者

SELECT * FROM Websites ORDER BY alexa;

执行结果:
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 3  | 编程宝库      | http://www.codebaoku.com/ | 4689  | CN      |
+----+--------------+---------------------------+-------+---------+

 

3. ORDER BY DESC 范例

下面的 SQL 语句从 "Websites" 表中选取所有网站,并按照 "alexa" 列降序排序:

SELECT * FROM Websites ORDER BY alexa;

执行结果:
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 3  | 编程宝库      | http://www.codebaoku.com/ | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
+----+--------------+---------------------------+-------+---------+

 

4. ORDER BY 多列 范例

下面的 SQL 语句从 "Websites" 表中选取所有网站,并按照 "country" 和 "alexa" 列排序:

SELECT * FROM Websites ORDER BY country,alexa;
执行结果:
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 3  | 编程宝库      | http://www.codebaoku.com/ | 4689  | CN      |
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

INSERT 语句用于向数据库的表中插入新记录。INSERT 语句可以有两种编写形式。第一种形式无需指定要插入数据的列名,只需提供被插入的值。第二种形式需要指定列名及被插入的值。