信息发布→ 登录 注册 退出

MySQL限制查询和数据排序介绍

发布时间:2026-01-11

点击量:
目录
  • 一、限制查询
    • 1.指定从第几行起,返回多少行
    • 2.取最大值
    • 3.取最小值
    • 4.分页
  • 二、数据排序
    • 1.年龄大于10的根据id进行降序排序
    • 2.年龄大于10的按照id进行升序排序

一、限制查询

我们通过limit可以限制返回结果的行数

select * from 表名 limit count;
select * from users limit 3;

1.指定从第几行起,返回多少行

select * from 表名 limit start,count;
select * from users limit 2,3;

相等

select * from users limit 3 offset 2;

2.取最大值

select * from users order by age desc limit 1;

3.取最小值

select * from users order by age asc limit 1;

4.分页

select * from users limit (page-1)*pageSize,pageSize;

二、数据排序

当数据查询出来以后,我们可以对数据进行排序处理。在末尾使用order by语句。

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

注:

  • asc即为升序,也是默认值。
  • desc即为降序
  • 排序首先先按照列1进行排序,如果出现结果相同的,在进行列2排序

1.年龄大于10的根据id进行降序排序

select * from users
where age > 10 order by id desc;

2.年龄大于10的按照id进行升序排序

select * from users
where age > 10 order by id asc;

在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!