day3|第05章_排序与分页|2022-11-14

day3|120天搞定mysql|2022-11-14

今天也是充满希望的一天

第05章_排序与分页

1.排序

        如果没有使用排序操作,默认情况下查询返回的数据是按照添加数据的顺序来显示的。

select * from employees;

# 1.1 基本使用

# 使用 order by 对查询到的数据进行排序操作。

# 升序:asc(ascend)

# 降序:desc(descend)

# 练习 :按照salary从高到低的顺序显示员工信息

mysql> select employee_id, last_name, salary from employees order by salary desc;

# 练习:按照salary从低到高的顺序显示员工信息

mysql> select  employee_id, last_name, salary from employees order by salary asc;

mysql> select  employee_id, last_name, salary from employees order by salary;

# 1.2 我们可以使用列的别名,进行排序

mysql> select employee_id, salary, salary*12 as "annual_sal" from employees order
by annual_sal;

#列的别名只能在 ORDER BY 中使用,不能在WHERE中使用。
#如下操作报错!
mysql> select employee_id, salary, salary*12 annual_sal from employees where annual_sal > 10000;

# 1.3 强调格式:where 需要声明在from之后,order by 之前。

mysql> select department_id,  employee_id, salary from employees where department_
id in (50, 60, 70) order by department_id desc;

# 1.4 二级排序/多级排序也是一样的

mysql> select employee_id, salary, department_id from employees order by department_id desc, salary asc;

2. 分页

# 2.1 mysql使用limit实现数据的分页显示

# 需求1:每页显示20条记录,此时显示第一页

mysql> select employee_id ,last_name from employees limit 0,20;

# 需求2:每页显示20条记录,此时显示第二页

mysql> select employee_id ,last_name from employees limit 20,20;

# 需求3:每页显示20条记录,此时显示第三页

mysql> select employee_id ,last_name from employees limit 40,20;

# 需求:每页显示pageSize条记录,此时显示第pageNo页

# 公式: limit (pageNo - 1)* pageSize, pageSize

# 2.2 where ... order by ... limit 声明的顺序如下:

# limit的格式:严格来说是: limit 位置偏移量,条目数

# 结构 "limit 0, 条目数" 等价于 "limit 条目数“

mysql> select employee_id, last_name, salary from employees where salary > 6000 order by salary desc limit 0, 10;


mysql> select employee_id, last_name, salary from employees where salary > 6000 order by salary desc limit 10;

# 练习:表里面有107条数据,我们只想要显示第32、33条数据怎么办呢?

mysql> select employee_id, last_name from employees limit 31, 2;

# 2.3 MySql8.0 新特性:limit ... offset...

#练习:表里有107条数据,我们只想要显示第 32、33 条数据怎么办呢?

mysql> select employee_id, last_name from employees limit 2 offset 31;

#练习:查询员工表中工资最高的员工信息

mysql> select employee_id, last_name, salary from employees order by salary desc limit 0, 1;


#2.4 LIMIT 可以使用在MySQL、PGSQL、MariaDB、SQLite 等数据库中使用,表示分页。
# 不能使用在SQL Server、DB2、Oracle!

第05章_排序与分页的课后练习

#1. 查询员工的姓名和部门号和年薪,按年薪降序,按姓名升序显示

mysql> select last_name, department_id, salary*12 as "annual_salary" from employees order by annual_salary desc, last_name asc;

#2. 选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第21到40位置的数据

mysql> select last_name, salary from employees where salary not between 8000 and 17000 order by salary desc limit 20, 20;

#3. 查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号升序

mysql> select employee_id, last_name, email, department_id from employees where email like '%e%' order by length(email) desc, department_id asc;

mysql> select employee_id, last_name, email, department_id from employees where email regexp '[e]' order by length(email) desc, department_id asc;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值