排序查询_OrderBy

1 语法:
select * from table order by <排序字段1> <排序方式1>,<排序字段2> <排序方式2>...
1.1 排序方式:
ASC:从小到大,升序,默认
DESC:从大到小,降序
1.2 当存在多个排序字段时,满足对第一个字段排序后,如果有相同的,再对相同的按排序字段2进行排序。
2 例子

mysql> select * from employee;
+----+------+------+--------+--------+--------+
| id | name | age  | salary | phone  | in_dpt |
+----+------+------+--------+--------+--------+
|  1 | Tom  |   26 |   2500 | 119119 | dpt4   |
|  2 | Jack |   24 |   2500 | 120120 | dpt2   |
|  3 | Rose |   22 |   2800 | 114114 | dpt3   |
|  4 | Jim  |   35 |   3000 | 100861 | dpt1   |
|  5 | Mary |   21 |   3000 | 100101 | dpt2   |
|  6 | Alex |   26 |   3000 | 123456 | dpt1   |
|  7 | Ken  |   27 |   3500 | 654321 | dpt1   |
|  8 | Rick |   24 |   3500 | 987654 | dpt3   |
|  9 | Joe  |   31 |   3600 | 110129 | dpt2   |
| 10 | Mike |   23 |   3400 | 110110 | dpt4   |
| 11 | Jobs | NULL |   3600 |  19283 | dpt2   |
| 12 | Tony | NULL |   3400 | 102938 | dpt3   |
+----+------+------+--------+--------+--------+
12 rows in set (0.00 sec)

2.1 将salary默认按升序排列

mysql> select * from employee order by salary;
+----+------+------+--------+--------+--------+
| id | name | age  | salary | phone  | in_dpt |
+----+------+------+--------+--------+--------+
|  1 | Tom  |   26 |   2500 | 119119 | dpt4   |
|  2 | Jack |   24 |   2500 | 120120 | dpt2   |
|  3 | Rose |   22 |   2800 | 114114 | dpt3   |
|  4 | Jim  |   35 |   3000 | 100861 | dpt1   |
|  5 | Mary |   21 |   3000 | 100101 | dpt2   |
|  6 | Alex |   26 |   3000 | 123456 | dpt1   |
| 10 | Mike |   23 |   3400 | 110110 | dpt4   |
| 12 | Tony | NULL |   3400 | 102938 | dpt3   |
|  7 | Ken  |   27 |   3500 | 654321 | dpt1   |
|  8 | Rick |   24 |   3500 | 987654 | dpt3   |
|  9 | Joe  |   31 |   3600 | 110129 | dpt2   |
| 11 | Jobs | NULL |   3600 |  19283 | dpt2   |
+----+------+------+--------+--------+--------+
12 rows in set (0.00 sec)

mysql> select * from employee order by salary,age desc;
+----+------+------+--------+--------+--------+

2.1 升序排完salary后,将salary相同的按age的降序排序

mysql> select * from employee order by salary,age desc;
+----+------+------+--------+--------+--------+
| id | name | age  | salary | phone  | in_dpt |
+----+------+------+--------+--------+--------+
|  1 | Tom  |   26 |   2500 | 119119 | dpt4   |
|  2 | Jack |   24 |   2500 | 120120 | dpt2   |
|  3 | Rose |   22 |   2800 | 114114 | dpt3   |
|  4 | Jim  |   35 |   3000 | 100861 | dpt1   |
|  6 | Alex |   26 |   3000 | 123456 | dpt1   |
|  5 | Mary |   21 |   3000 | 100101 | dpt2   |
| 10 | Mike |   23 |   3400 | 110110 | dpt4   |
| 12 | Tony | NULL |   3400 | 102938 | dpt3   |
|  7 | Ken  |   27 |   3500 | 654321 | dpt1   |
|  8 | Rick |   24 |   3500 | 987654 | dpt3   |
|  9 | Joe  |   31 |   3600 | 110129 | dpt2   |
| 11 | Jobs | NULL |   3600 |  19283 | dpt2   |
+----+------+------+--------+--------+--------+
12 rows in set (0.01 sec)

2.2 升序排完salary后,将salary相同的按age的升序排序

mysql> select * from employee order by salary,age;
+----+------+------+--------+--------+--------+
| id | name | age  | salary | phone  | in_dpt |
+----+------+------+--------+--------+--------+
|  2 | Jack |   24 |   2500 | 120120 | dpt2   |
|  1 | Tom  |   26 |   2500 | 119119 | dpt4   |
|  3 | Rose |   22 |   2800 | 114114 | dpt3   |
|  5 | Mary |   21 |   3000 | 100101 | dpt2   |
|  6 | Alex |   26 |   3000 | 123456 | dpt1   |
|  4 | Jim  |   35 |   3000 | 100861 | dpt1   |
| 12 | Tony | NULL |   3400 | 102938 | dpt3   |
| 10 | Mike |   23 |   3400 | 110110 | dpt4   |
|  8 | Rick |   24 |   3500 | 987654 | dpt3   |
|  7 | Ken  |   27 |   3500 | 654321 | dpt1   |
| 11 | Jobs | NULL |   3600 |  19283 | dpt2   |
|  9 | Joe  |   31 |   3600 | 110129 | dpt2   |
+----+------+------+--------+--------+--------+
12 rows in set (0.00 sec)

2.3 在age排序后没有相同的age,再对in_dpt排序是无效的

mysql> select * from employee order by salary,age,in_dpt;
+----+------+------+--------+--------+--------+
| id | name | age  | salary | phone  | in_dpt |
+----+------+------+--------+--------+--------+
|  2 | Jack |   24 |   2500 | 120120 | dpt2   |
|  1 | Tom  |   26 |   2500 | 119119 | dpt4   |
|  3 | Rose |   22 |   2800 | 114114 | dpt3   |
|  5 | Mary |   21 |   3000 | 100101 | dpt2   |
|  6 | Alex |   26 |   3000 | 123456 | dpt1   |
|  4 | Jim  |   35 |   3000 | 100861 | dpt1   |
| 12 | Tony | NULL |   3400 | 102938 | dpt3   |
| 10 | Mike |   23 |   3400 | 110110 | dpt4   |
|  8 | Rick |   24 |   3500 | 987654 | dpt3   |
|  7 | Ken  |   27 |   3500 | 654321 | dpt1   |
| 11 | Jobs | NULL |   3600 |  19283 | dpt2   |
|  9 | Joe  |   31 |   3600 | 110129 | dpt2   |
+----+------+------+--------+--------+--------+
12 rows in set (0.00 sec)

你可能感兴趣的:(排序查询_OrderBy)