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,column_name ASC|DESC;
演示数据库
在本教程中,我们将使用 runoops样本数据库。
下面是选自 “websites” 表的数据:
+----+----------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+----------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Amazon | https://z.cn/ | 2 | USA |
| 3 | 淘宝 | https://www.taobao.com/ | 10 | CN |
| 4 | 自学教程 | http://runoops.com/ | 5787 | CN |
| 5 | 微博 | http://weibo.com/ | 18 | CN |
| 6 | stackoverflow | http://stackoverflow.com/ | 66 | IND |
+----+----------------+---------------------------+-------+---------+
ORDER BY 实例
下面的 SQL 语句从 "websites" 表中选取所有网站,并按照 "alexa" 列排序:
SELECT * FROM websites
ORDER BY alexa;
执行输出结果:
mysql> SELECT * FROM websites
-> ORDER BY alexa;
+----+----------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+----------------+---------------------------+-------+---------+
| 1 | Goole | https://www.google.cm/ | 1 | USA |
| 2 | Amazon | https://z.cn/ | 2 | USA |
| 3 | 淘宝 | https://www.taobao.com/ | 10 | CN |
| 5 | 微博 | http://weibo.com/ | 18 | CN |
| 6 | stackoverflow | http://stackoverflow.com/ | 66 | IND |
| 4 | 自学教程 | http://runoops.com/ | 5787 | CN |
+----+----------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)
ORDER BY DESC 实例
下面的 SQL 语句从 "websites" 表中选取所有网站,并按照 "alexa" 列降序排序:
SELECT * FROM websites
ORDER BY alexa DESC;
执行输出结果:
mysql> SELECT * FROM websites
-> ORDER BY alexa DESC;
+----+----------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+----------------+---------------------------+-------+---------+
| 4 | 自学教程 | http://runoops.com/ | 5787 | CN |
| 6 | stackoverflow | http://stackoverflow.com/ | 66 | IND |
| 5 | 微博 | http://weibo.com/ | 18 | CN |
| 3 | 淘宝 | https://www.taobao.com/ | 10 | CN |
| 2 | Amazon | https://z.cn/ | 2 | USA |
| 1 | Google | https://www.google.cm/ | 1 | USA |
+----+----------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)
ORDER BY 多列
下面的 SQL 语句从 "websites" 表中选取所有网站,并按照 "country" 和 "alexa" 列排序:
SELECT * FROM websites
ORDER BY country,alexa;
执行输出结果:
mysql> SELECT * FROM websites
-> ORDER BY country,alexa;
+----+----------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+----------------+---------------------------+-------+---------+
| 3 | 淘宝 | https://www.taobao.com/ | 10 | CN |
| 5 | 微博 | http://weibo.com/ | 18 | CN |
| 4 | 自学教程 | http://runoops.com/ | 5787 | CN |
| 6 | stackoverflow | http://stackoverflow.com/ | 66 | IND |
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Amazon | https://z.cn/ | 2 | USA |
+----+----------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)