在MySQL中,使用ORDER BY
关键字可以对查询结果进行排序。ORDER BY
后面可以跟一个或多个列名,用于指定排序的依据。你可以使用ASC
(升序,默认)或DESC
(降序)关键字来指定排序顺序。
以下是一些使用ORDER BY
关键字的示例:
SELECT * FROM table_name ORDER BY column_name ASC;
SELECT * FROM table_name ORDER BY column_name DESC;
SELECT * FROM table_name ORDER BY column1_name ASC, column2_name ASC;
SELECT * FROM table_name ORDER BY column1_name DESC, column2_name DESC;
SELECT column1_name AS alias1, column2_name AS alias2 FROM table_name ORDER BY alias1 ASC, alias2 DESC;
LIMIT
子句限制排序后的结果数量:SELECT * FROM table_name ORDER BY column_name DESC LIMIT 10;
这些示例展示了如何使用MySQL的ORDER BY
关键字对查询结果进行排序。你可以根据自己的需求调整这些示例以满足特定的排序要求。