读书笔记--SQL必知必会03--排序检索数据

时间:2022-06-01 14:10:51

3.1 排序数据

子句(clause)

SQL语句由子句构成。一个子句通常由一个关键字加上所提供的数据组成。

ORDER BY子句可以取一个或多个列的名字,将SELECT语句检索出的数据进行排序。

ORDER BY子句可以使用非检索的列排序数据。

ORDER BY子句必须作为SELECT语句中最后一条子句。

MariaDB [sqlbzbh]> SELECT prod_name FROM Products ORDER BY prod_name;
+---------------------+
| prod_name |
+---------------------+
| 12 inch teddy bear |
| 18 inch teddy bear |
| 8 inch teddy bear |
| Bird bean bag toy |
| Fish bean bag toy |
| King doll |
| Queen doll |
| Rabbit bean bag toy |
| Raggedy Ann |
+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT prod_name FROM Products ORDER BY vend_id;
+---------------------+
| prod_name |
+---------------------+
| 8 inch teddy bear |
| 12 inch teddy bear |
| 18 inch teddy bear |
| Fish bean bag toy |
| Bird bean bag toy |
| Rabbit bean bag toy |
| Raggedy Ann |
| King doll |
| Queen doll |
+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>

3.2 按多个列排序

要按多个列排序,简单指定列名,列名之间用逗号分开即可。

在按多个列排序时,排序的顺序完全按规定进行。也就是说,仅在前一列的多个行具有相同的值时,才按照下一列进行排序。

MariaDB [sqlbzbh]> SELECT prod_id,prod_price,prod_name FROM Products ORDER BY prod_price,prod_name;
+---------+------------+---------------------+
| prod_id | prod_price | prod_name |
+---------+------------+---------------------+
| BNBG02 | 3.49 | Bird bean bag toy |
| BNBG01 | 3.49 | Fish bean bag toy |
| BNBG03 | 3.49 | Rabbit bean bag toy |
| RGAN01 | 4.99 | Raggedy Ann |
| BR01 | 5.99 | 8 inch teddy bear |
| BR02 | 8.99 | 12 inch teddy bear |
| RYL01 | 9.49 | King doll |
| RYL02 | 9.49 | Queen doll |
| BR03 | 11.99 | 18 inch teddy bear |
+---------+------------+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>

3.3 按列位置排序

ORDER BY支持按相对列位置进行排序。

优点是不用重新输入列名,缺点是容易造成错误排序。个人不建议使用。

如果有必要,可以混合使用实际列名和相对列位置。

MariaDB [sqlbzbh]> SELECT prod_id,prod_price,prod_name FROM Products ORDER BY 2,3;
+---------+------------+---------------------+
| prod_id | prod_price | prod_name |
+---------+------------+---------------------+
| BNBG02 | 3.49 | Bird bean bag toy |
| BNBG01 | 3.49 | Fish bean bag toy |
| BNBG03 | 3.49 | Rabbit bean bag toy |
| RGAN01 | 4.99 | Raggedy Ann |
| BR01 | 5.99 | 8 inch teddy bear |
| BR02 | 8.99 | 12 inch teddy bear |
| RYL01 | 9.49 | King doll |
| RYL02 | 9.49 | Queen doll |
| BR03 | 11.99 | 18 inch teddy bear |
+---------+------------+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>

3.4 制定排序方向

默认的排序顺序是升序排序(从A到Z)。

可以使用ORDER BY子句进行降序排序(从Z到A),但必须指定DESC关键字。

DESC关键字只应用到直接位于其前面的列名。如果想在多个列上进行降序排序,必须对每一列指定DESC关键字。

DESC是DESCENDING的缩写,这两个关键字都可以使用。

ASC是ASCENDING的缩写,因为升序是默认的,所以不必指定关键字。

MariaDB [sqlbzbh]> SELECT prod_id,prod_price,prod_name FROM Products ORDER BY prod_price;
+---------+------------+---------------------+
| prod_id | prod_price | prod_name |
+---------+------------+---------------------+
| BNBG01 | 3.49 | Fish bean bag toy |
| BNBG02 | 3.49 | Bird bean bag toy |
| BNBG03 | 3.49 | Rabbit bean bag toy |
| RGAN01 | 4.99 | Raggedy Ann |
| BR01 | 5.99 | 8 inch teddy bear |
| BR02 | 8.99 | 12 inch teddy bear |
| RYL01 | 9.49 | King doll |
| RYL02 | 9.49 | Queen doll |
| BR03 | 11.99 | 18 inch teddy bear |
+---------+------------+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT prod_id,prod_price,prod_name FROM Products ORDER BY prod_price DESC;
+---------+------------+---------------------+
| prod_id | prod_price | prod_name |
+---------+------------+---------------------+
| BR03 | 11.99 | 18 inch teddy bear |
| RYL01 | 9.49 | King doll |
| RYL02 | 9.49 | Queen doll |
| BR02 | 8.99 | 12 inch teddy bear |
| BR01 | 5.99 | 8 inch teddy bear |
| RGAN01 | 4.99 | Raggedy Ann |
| BNBG01 | 3.49 | Fish bean bag toy |
| BNBG02 | 3.49 | Bird bean bag toy |
| BNBG03 | 3.49 | Rabbit bean bag toy |
+---------+------------+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>
MariaDB [sqlbzbh]> SELECT prod_id,prod_price,prod_name FROM Products ORDER BY prod_price DESC, prod_name;
+---------+------------+---------------------+
| prod_id | prod_price | prod_name |
+---------+------------+---------------------+
| BR03 | 11.99 | 18 inch teddy bear |
| RYL01 | 9.49 | King doll |
| RYL02 | 9.49 | Queen doll |
| BR02 | 8.99 | 12 inch teddy bear |
| BR01 | 5.99 | 8 inch teddy bear |
| RGAN01 | 4.99 | Raggedy Ann |
| BNBG02 | 3.49 | Bird bean bag toy |
| BNBG01 | 3.49 | Fish bean bag toy |
| BNBG03 | 3.49 | Rabbit bean bag toy |
+---------+------------+---------------------+
9 rows in set (0.00 sec) MariaDB [sqlbzbh]>