排序和分页
- 一、排序
- 1.简单用法
- 3.不同字段不同排序现实
- 二、分页
- 1.简单分页
- 2.order by 配合limit
- 三、分页8.0新特性
- 1.offset
- 总结
提示:以下是本篇文章正文内容
一、排序
1.简单用法
select employee_id,last_name,salary from employees order by salary;
默认是升序
select employee_id,last_name,salary from employees order by salary asc;
select employee_id,last_name,salary from employees order by salary desc;
order by 用别名进行排序
select employee_id,salary,salary * 12 annual_sal from employees order by annual_sal;
2.where和order by
select employee_id,salary,salary * 12 annual_sal from employees where department_id in (50,60,70) order by annual_sal desc;
3.不同字段不同排序现实
部门id升序排序,相同部门id的员工薪资降序排序
select employee_id,department_id,salary,salary * 12 annual_sal from employees order by department_id asc , salary desc;
二、分页
1.简单分页
每页现实20条记录,此时现实第一页
select employee_id,last_name from employees limit 0,20;
查看第二页,以此类推
select employee_id,last_name from employees order by employee_id limit 20,20;
得出一个公式
2.order by 配合limit
前面显示的数据没有排序,数据很乱。
三、分页8.0新特性
1.offset
select employee_id,last_name from employees order by employee_id limit 20 offset 0;
查询员工里工资最高和最低的员工信息
select employee_id,last_name,salary from employees order by salary asc limit 1 offset 0;
select employee_id,last_name,salary from employees order by salary desc limit 1 offset 0;
注意事项:
总结
排序和分页的基本操作差不多都在这里了,后续会有更多内容,希望大家喜欢!