SQL ORDER BY 关键字

SQL 教程 · 2019-03-03 15:56:37

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;

演示数据库

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

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

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

ORDER BY 实例

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

实例

SELECT * FROM Websites ORDER BY alexa;

ORDER BY DESC 实例

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

实例

SELECT * FROM Websites ORDER BY alexa DESC;

ORDER BY 多列

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

实例

SELECT * FROM Websites ORDER BY country,alexa;

点击查看所有 SQL 教程 文章: https://codercto.com/courses/l/29.html

查看所有标签

Algorithms Illuminated (Part 2)

Algorithms Illuminated (Part 2)

Tim Roughgarden / Soundlikeyourself Publishing, LLC / 2018-8-5 / USD 17.99

Algorithms are the heart and soul of computer science. Their applications range from network routing and computational genomics to public-key cryptography and machine learning. Studying algorithms can......一起来看看 《Algorithms Illuminated (Part 2)》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具