SQL ORDER BY 子句根据一列或者多列的值,按照升序或者降序排列数据。某些数据库默认以升序排列查询结果。
ORDER BY 子句的基本语法如下所示:
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
ORDER BY 子句可以同时使用多个列作为排序条件。无论用哪一列作为排序条件,都要确保该列在存在。
考虑含有如下所示记录的 CUSTOMERS 表:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
下面的例子将查询结果按照 NAME 和 SALARY 升序排列:
SQL> SELECT * FROM CUSTOMERS
ORDER BY NAME, SALARY;
结果如下所示:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+
下面的例子将查询结果按照 NAME 降序排列:
SQL> SELECT * FROM CUSTOMERS
ORDER BY NAME DESC;
结果如下所示:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+
SQL UNION 子句 SQL UNION 子句/运算符用于将两个或者更多的 SELECT 语句的运算结果组合起来。 在使用 UNION ...
C++ goto 语句 C++ 循环goto 语句允许把控制无条件转移到同一函数内的被标记的语句。注意:在任何编程语言中,都不建议使用 goto...
C++ 基本的输入输出C++ 标准库提供了一组丰富的输入/输出功能,我们将在后续的章节进行介绍。本章将讨论 C++ 编程中最基本和最常...
C 库函数 - printf() C 标准库 - stdio.h描述C 库函数 int printf(const char *format, ...) 发送格式化输出到标准输出 stdout。...