#进阶8 分页查询 /* 应用场景: 当要显示的数据,一页显示不全,需要分页提交sql请求 语法: select 查询列表 #7 from 表1 #执行顺序:#1 【join type join 表2 #2 on 连接条件 #3 where 筛选条件 #4 group by 分组字段 #5 having 分组后筛选或函数 #6 order by 排序的字段 #8 】 limit offset, size ; #9 #offset 要显示条目的起始索引(从0开始,0想可以省略),size 要显示的条目个数 特点: ①limit语句放在查询语句的,执行上语法上都在最后 ②公式, 要显示的页数page ,每页的条目数 size select 查询列表 from 表 limit(page-1)*size ,size; */ #案例1: 查询前五条员工信息 SELECT * FROM employees LIMIT 0,5; SELECT * FROM employees LIMIT 5; #案例2:查询第11到第25条 SELECT * FROM employees LIMIT 11,15; # 案例3:有奖金的员工信息,并且工资较高的前10项显示出来 SELECT * FROM employees WHERE commission_pct IS NOT NULL ORDER BY salary DESC LIMIT 10; #-------------------------- #课堂练习 #1.查询所有学员的邮箱的用户名(注: 邮箱中 @前的字符) ;instr() : 返回子串的索引下标 SELECT SUBSTR( email,1,INSTR(email,'@')-1) FROM stuinfo; #------------------------------------ #子查询的经典案例 #1:查询工资最低的员工信息: last_name,salary SELECT last_name,salary FROM employees WHERE salary=( SELECT MIN(salary) FROM employees ); #2:查询平均工资最低的部门信息 #方式1: #分组查询平均工资 #查询最低的一组 #查询那个部门最低平均工资 = 最低的一组 SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id; SELECT MIN(ag) FROM ( SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id )ag_dep; SELECT *,AVG(salary) FROM employees GROUP BY department_id HAVING AVG(salary) = ( SELECT MIN(ag) FROM ( SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id )ag_dep ); #方式2: #①分组查询平均工资 SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id; #②求出最低的(limit=1)平均工资的部门编号 SELECT department_id FROM employees GROUP BY department_id ORDER BY AVG(salary) LIMIT 1; #③ 进行嵌套,输出该部门信息 SELECT * FROM departments WHERE department_id =( SELECT department_id FROM employees GROUP BY department_id ORDER BY AVG(salary) LIMIT 1 ); #3: 查询平均工资高于公司的平均工资的部门有哪些 #①查询平均工资 SELECT AVG(salary) FROM employees; #② 查询每个部门的 SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id; #③ 进行筛选2的结果集: > ① 即可! SELECT *,AVG(salary) ag,department_id FROM employees GROUP BY department_id HAVING ag >( SELECT AVG(salary) FROM employees );