MySQL排序

MySQL大约 2433 字

order by

select * from 表名 order by 列1 asc|desc,列2 asc|desc,...

select * from user order by age; (升序)

+----+-------+------+---------------------+--------+------+----------+
| id | name  | age  | birthday            | gender | home | isdelete |
+----+-------+------+---------------------+--------+------+----------+
| 11 | admin |    0 | NULL                | NULL   | NULL | NULL     |
|  6 | 曹值     |   18 | 2000-01-01 00:00:00 |       | 魏    |          |
|  5 | 曹丕     |   20 | 2000-01-01 00:00:00 |       | 魏    |          |
| 10 | 小乔     |   22 | 2000-01-01 00:00:00 |        | 吴     |          |
|  9 | 大乔      |   23 | 2000-01-01 00:00:00 |        | 吴     |          |
|  7 | 孙权     |   25 | 2000-01-01 00:00:00 |       | 吴     |          |
|  8 | 周瑜      |   25 | 2000-01-01 00:00:00 |       | 吴     |          |
|  3 | 张飞     |   35 | 2000-01-01 00:00:00 |       | 蜀     |          |
|  2 | 关羽      |   36 | 2000-01-01 00:00:00 |       | 蜀     |          |
|  1 | 刘备      |   37 | 2000-01-01 00:00:00 |       | 蜀     |          |
|  4 | 曹操     |   40 | 2000-01-01 00:00:00 |       | 魏    |          |
+----+-------+------+---------------------+--------+------+----------+
11 rows in set (0.00 sec)

默认按照列值从小到大排列

某些行列1的值相同时,则按照列2排序,以此类推

  • asc从小到大排列,即升序
  • desc从大到小排序,即降序
select * from user order by age desc;

+----+-------+------+---------------------+--------+------+----------+
| id | name  | age  | birthday            | gender | home | isdelete |
+----+-------+------+---------------------+--------+------+----------+
|  4 | 曹操     |   40 | 2000-01-01 00:00:00 |       | 魏    |          |
|  1 | 刘备      |   37 | 2000-01-01 00:00:00 |       | 蜀     |          |
|  2 | 关羽      |   36 | 2000-01-01 00:00:00 |       | 蜀     |          |
|  3 | 张飞     |   35 | 2000-01-01 00:00:00 |       | 蜀     |          |
|  7 | 孙权     |   25 | 2000-01-01 00:00:00 |       | 吴     |          |
|  8 | 周瑜      |   25 | 2000-01-01 00:00:00 |       | 吴     |          |
|  9 | 大乔      |   23 | 2000-01-01 00:00:00 |        | 吴     |          |
| 10 | 小乔     |   22 | 2000-01-01 00:00:00 |        | 吴     |          |
|  5 | 曹丕     |   20 | 2000-01-01 00:00:00 |       | 魏    |          |
|  6 | 曹值     |   18 | 2000-01-01 00:00:00 |       | 魏    |          |
| 11 | admin |    0 | NULL                | NULL   | NULL | NULL     |
+----+-------+------+---------------------+--------+------+----------+
11 rows in set (0.00 sec)
阅读 327 · 发布于 2019-04-07

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批