玩转Mysql系列 - 第8篇:详解排序和分页(order by & limit),及存在的坑(1)

本文详细介绍了MySQL中的排序(order by)和分页(limit)操作,包括单字段、多字段、别名排序、函数排序以及分页查询的使用方法,并通过实例展示了如何避免常见错误。同时,文章提供了Kafka实战笔记的概览。
摘要由CSDN通过智能技术生成

select 字段名 from 表名 order by 字段1 [asc|desc],字段2 [asc|desc];

需要排序的字段跟在order by之后;

asc|desc表示排序的规则,asc:升序,desc:降序,默认为asc;

支持多个字段进行排序,多字段排序之间用逗号隔开。

单字段排序

mysql> create table test2(a int,b varchar(10));

Query OK, 0 rows affected (0.01 sec)

mysql> insert into test2 values (10,‘jack’),(8,‘tom’),(5,‘ready’),(100,‘javacode’);

Query OK, 4 rows affected (0.00 sec)

Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from test2;

±-----±---------+

| a    | b        |

±-----±---------+

|   10 | jack     |

|    8 | tom      |

|    5 | ready    |

|  100 | javacode |

±-----±---------+

4 rows in set (0.00 sec)

mysql> select * from test2 order by a asc;

±-----±---------+

| a    | b        |

±-----±---------+

|    5 | ready    |

|    8 | tom      |

|   10 | jack     |

|  100 | javacode |

±-----±---------+

4 rows in set (0.00 sec)

mysql> select * from test2 order by a desc;

±-----±---------+

| a    | b        |

±-----±---------+

|  100 | javacode |

|   10 | jack     |

|    8 | tom      |

|    5 | ready    |

±-----±---------+

4 rows in set (0.00 sec)

mysql> select * from test2 order by a;

±-----±---------+

| a    | b        |

±-----±---------+

|    5 | ready    |

|    8 | tom      |

|   10 | jack     |

|  100 | javacode |

±-----±---------+

4 rows in set (0.00 sec)

多字段排序

比如学生表,先按学生年龄降序,年龄相同时,再按学号升序,如下:

mysql> create table stu(id int not null comment ‘学号’ primary key,age tinyint not null comment ‘年龄’,name varchar(16) comment ‘姓名’);

Query OK, 0 rows affected (0.01 sec)

mysql> insert into stu (id,age,name) values (1001,18,‘路人甲Java’),(1005,20,‘刘德华’),(1003,18,‘张学友’),(1004,20,‘张国荣’),(1010,19,‘梁朝伟’);

Query OK, 5 rows affected (0.00 sec)

Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from stu;

±-----±----±--------------+

| id   | age | name          |

±-----±----±--------------+

| 1001 |  18 | 路人甲Java    |

| 1003 |  18 | 张学友        |

| 1004 |  20 | 张国荣        |

| 1005 |  20 | 刘德华        |

| 1010 |  19 | 梁朝伟        |

±-----±----±--------------+

5 rows in set (0.00 sec)

mysql> select * from stu order by age desc,id asc;

±-----±----±--------------+

| id   | age | name          |

±-----±----±--------------+

| 1004 |  20 | 张国荣        |

| 1005 |  20 | 刘德华        |

| 1010 |  19 | 梁朝伟        |

| 1001 |  18 | 路人甲Java    |

| 1003 |  18 | 张学友        |

±-----±----±--------------+

5 rows in set (0.00 sec)

按别名排序

mysql> select * from stu;

±-----±----±--------------+

| id   | age | name          |

±-----±----±--------------+

| 1001 |  18 | 路人甲Java    |

| 1003 |  18 | 张学友        |

| 1004 |  20 | 张国荣        |

| 1005 |  20 | 刘德华        |

| 1010 |  19 | 梁朝伟        |

±-----±----±--------------+

5 rows in set (0.00 sec)

mysql> select age ‘年龄’,id as ‘学号’ from stu order by 年龄 asc,学号 desc;

±-------±-------+

| 年龄   | 学号   |

±-------±-------+

|     18 |   1003 |

|     18 |   1001 |

|     19 |   1010 |

|     20 |   1005 |

|     20 |   1004 |

±-------±-------+

按函数排序

有学生表(i

  • 25
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值