SQL ORDER BY 关键字
ORDER BY
关键字用于将结果集按升序或降序排序。
ORDER BY
关键字默认以升序对记录进行排序。要以降序排序,请使用DESC
关键字。
ORDER BY 语法
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY 示例
下面的SQL语句从 "Customers"表中选择所有的客户,按 "Country"列排序:
SELECT * FROM Customers
ORDER BY Country LIMIT 5;
输出:
ORDER BY DESC 示例
下面的SQL语句从 "Customers"表中选择所有客户,按 "Country"列进行降序排列:
SELECT * FROM Customers
ORDER BY Country DESC LIMIT 5;
输出:
ORDER BY若干列示例
下面的SQL语句从 "Customers "表中选择所有客户,按照 "Country "和 "CustomerName "列进行排序。这意味着它按国家排序,但如果一些行有相同的国家,它就按客户名称排序:
SELECT * FROM Customers
ORDER BY Country, CustomerName LIMIT 5;
输出:
ORDER BY若干列示例2
下面的SQL语句从 "Customers"表中选择所有客户,按 "Country"升序排序,按 "CustomerName"列降序排序:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC LIMIT 10;
输出: