📜  SQL ORDER BY

📅  最后修改于: 2020-11-11 04:49:57             🧑  作者: Mango

SQL ORDER BY子句

SQL ORDER BY子句用于根据一个或多个列以升序和降序对数据进行排序。

默认情况下,某些数据库以升序对查询结果进行排序。

SQL ORDER BY语法:

SELECT expressions
FROM tables
WHERE conditions
ORDER BY expression [ASC | DESC];

让我们看一个具有以下记录的CUSTOMERS表:

ID NAME AGE ADDRESS SALARY
1 Himani gupta 21 Modinagar 22000
2 Shiva tiwari 22 Bhopal 21000
3 Ajeet bhargav 45 Meerut 65000
4 Ritesh yadav 36 Azamgarh 26000
5 Balwant singh 45 Varanasi 36000
6 Mahesh sharma 26 Mathura 22000

此示例将按照NAME和SALARY的升序对结果进行排序。

SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY;

这将产生以下结果。

ID NAME AGE ADDRESS SALARY
3 Ajeet bhargav 45 Meerut 65000
5 Balwant singh 45 Varanasi 36000
1 Himani gupta 21 Modinagar 22000
6 Mahesh sharma 26 Mathura 22000
4 Ritesh yadav 36 Azamgarh 26000
2 Shiva tiwari 22 Bhopal 21000

这是一个按名称降序对结果进行排序的示例。

SELECT * FROM CUSTOMERS
ORDER BY NAME DESC;

这将产生以下结果。

ID NAME AGE ADDRESS SALARY
2 Shiva tiwari 22 Bhopal 21000
4 Ritesh yadav 36 Azamgarh 26000
6 Mahesh sharma 26 Mathura 22000
1 Himani gupta 21 Modinagar 22000
5 Balwant singh 45 Varanasi 36000
3 Ajeet bhargav 45 Meerut 65000

SQL ORDER BY子句的主题