Mysql进阶学习(六)子查询与分页查询
进阶7:子查询
1、含义:
出现在其他语句中的select语句,称为子查询或内查询
外部的查询语句,称为主查询或外查询
2、分类:
按子查询出现的位置:
select后面:
仅仅支持标量子查询
from后面:
支持表子查询
where或having后面:★
标量子查询(单行) √
列子查询 (多行) √
行子查询
exists后面(相关子查询)
表子查询
按结果集的行列数不同:
标量子查询(结果集只有一行一列)
列子查询(结果集只有一列多行)
行子查询(结果集有一行多列)
表子查询(结果集一般为多行多列)
3、where或having后面
1、标量子查询(单行子查询)
2、列子查询(多行子查询)
3、行子查询(多列多行)
3.1 特点:
①子查询放在小括号内
②子查询一般放在条件的右侧
③标量子查询,一般搭配着单行操作符使用
< >= <= = <>
列子查询,一般搭配着多行操作符使用
in、any/some、all
④子查询的执行优先于主查询执行,主查询的条件用到了子查询的结果
3.2.标量子查询★
案例1:谁的工资比 Abel 高?
①查询Abel的工资
SELECT salary
FROM employees
WHERE last_name = 'Abel'
②查询员工的信息,满足 salary>①结果
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
案例2:返回job_id与141号员工相同,salary比143号员工多的员工 姓名,job_id 和工资
①查询141号员工的job_id
SELECT job_id
FROM employees
WHERE employee_id = 141
②查询143号员工的salary
SELECT salary
FROM employees
WHERE employee_id = 143
③查询员工的姓名,job_id 和工资,要求job_id=①并且salary>②
SELECT last_name,job_id,salary
FROM employees
WHERE job_id = (
SELECT job_id
FROM employees
WHERE employee_id = 141
) AND salary>(
SELECT salary
FROM employees
WHERE employee_id = 143
);
案例3:返回公司工资最少的员工的last_name,job_id和salary
①查询公司的 最低工资
SELECT MIN(salary)
FROM employees
②查询last_name,job_id和salary,要求salary=①
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
案例4:查询最低工资大于50号部门最低工资的部门id和其最低工资
①查询50号部门的最低工资
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
②查询每个部门的最低工资
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
③ 在②基础上筛选,满足min(salary)>①
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);
非法使用标量子查询
单行操作符只能搭配标量子查询
错误案例:
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT salary
FROM employees
WHERE department_id = 250
);
3.3.列子查询(多行子查询)★
使用多行比较操作符。
操作符 | 含义 |
---|---|
IN/NOT IN | 等于列表中的任意一个 |
ANY | SOME |
ALL | 和子查询返回的所有值比较 |
案例1:返回location_id是1400或1700的部门中的所有员工姓名
①查询location_id是1400或1700的部门编号
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
②查询员工姓名,要求部门号是①列表中的某一个
SELECT last_name
FROM employees
WHERE department_id <>ALL(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);
SELECT last_name
FROM employees
WHERE department_id IN (
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);
案例2:返回其它工种中比job_id为‘IT_PROG’工种任一工资低的员工的员工号、姓名、job_id 以及salary
①查询job_id为‘IT_PROG’部门任一工资
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
②查询员工号、姓名、job_id 以及salary,salary<(①)的任意一个
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
或
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MAX(salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
案例3:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工 的员工号、姓名、job_id 以及salary
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
或
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MIN( salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
注意:<>表示不等于
3.4. 行子查询(结果集一行多列或多行多列)
案例:查询员工编号最小并且工资最高的员工信息
SELECT *
FROM employees
WHERE (employee_id,salary)=(
SELECT MIN(employee_id),MAX(salary)
FROM employees
);
①查询最小的员工编号
SELECT MIN(employee_id)
FROM employees
②查询最高工资
SELECT MAX(salary)
FROM employees
③查询员工信息
SELECT *
FROM employees
WHERE employee_id=(
SELECT MIN(employee_id)
FROM employees
)AND salary=(
SELECT MAX(salary)
FROM employees
);
4、select后面
仅仅支持标量子查询
案例1:查询每个部门的员工个数
SELECT d.*,(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.`department_id`
) 个数
FROM departments d;
案例2:查询员工号=102的部门名
SELECT(
SELECT department_name
FROM departments d
INNER JOIN employees e
ON d.department_id=e.department_id
WHERE e.employee_id=102
) 部门名;
错误示例
(1)多行示例
select后面只能放单行单列不能多行多列,
SELECT(
SELECT department_name,d.department_id
FROM departments d
INNER JOIN employees e
ON d.department_id=e.department_id
WHERE e.employee_id=102
) 部门名;
错误代码:
查询:SELECT( SELECT department_name,d.department_id FROM departments d INNER JOIN employees e ON d.department_id=e.department_id WHER...错误代码: 1241
Operand should contain 1 column(s)
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0 sec
(2)多列示例
SELECT(
SELECT department_name
FROM departments d
INNER JOIN employees e
ON d.department_id=e.department_id
) 部门名;
错误代码
查询:SELECT( SELECT department_name FROM departments d INNER JOIN employees e ON d.department_id=e.department_id ) 部门名错误代码: 1242
Subquery returns more than 1 row
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0.001 sec
5、from后面
将子查询结果充当一张表,要求必须起别名
案例1:查询每个部门的平均工资的工资等级
① 查询每个部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
SELECT * FROM job_grades;
② 连接①的结果集和job_grades表,筛选条件平均工资 between lowest_sal and highest_sal
SELECT ag_dep.*,g.`grade_level`
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN lowest_sal AND highest_sal;
6、exists后面(相关子查询)
使用的比较少,可用之前的语法代替
语法:
exists:表示是否存在
exists(完整的查询语句) :表示后面语句是否存在
结果: 1或0
SELECT EXISTS(SELECT employee_id FROM employees WHERE salary=300000);
案例1:查询有员工的部门名
in
in表示:in前面的值与in后面的值任意匹配即可
SELECT department_name
FROM departments d
WHERE d.`department_id` IN(
SELECT department_id
FROM employees
)
exists
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE d.`department_id`=e.`department_id`
);
案例2:查询没有女朋友的男神信息
in
SELECT bo.*
FROM boys bo
WHERE bo.id NOT IN(
SELECT boyfriend_id
FROM beauty
)
exists
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS(
SELECT boyfriend_id
FROM beauty b
WHERE bo.`id`=b.`boyfriend_id`
);
7.案例练习
- 查询和 Zlotkey 相同部门的员工姓名和工资
- 查询工资比公司平均工资高的员工的员工号,姓名和工资。
- 查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
- 查询和姓名中包含字母 u 的员工在相同部门的员工的员工号和姓名
- 查询在部门的 location_id 为 1700 的部门工作的员工的员工号
- 查询管理者是 King 的员工姓名和工资
- 查询工资最高的员工的姓名,要求 first_name 和 last_name 显示为一列,列名为 姓.名
7.1. 查询和Zlotkey相同部门的员工姓名和工资
①查询Zlotkey的部门
SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey'
②查询部门号=①的姓名和工资
SELECT last_name,salary
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey'
)
7.2.查询工资比公司平均工资高的员工的员工号,姓名和工资。
① 查询平均工资
SELECT AVG(salary)
FROM employees
② 查询工资>①的员工号,姓名和工资。
SELECT last_name,employee_id,salary
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
);
7.3.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资
① 查询各部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
② 连接①结果集和employees表,进行筛选
SELECT employee_id,last_name,salary,e.department_id
FROM employees e
INNER JOIN (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
ON e.department_id = ag_dep.department_id
WHERE salary>ag_dep.ag ;
7.4. 查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
① 查询姓名中包含字母u的员工的部门
SELECT DISTINCT department_id
FROM employees
WHERE last_name LIKE '%u%'
② 查询部门号=①中的任意一个的员工号和姓名
SELECT last_name,employee_id
FROM employees
WHERE department_id IN(
SELECT DISTINCT department_id
FROM employees
WHERE last_name LIKE '%u%'
);
7.5. 查询在部门的location_id为1700的部门工作的员工的员工号
① 查询location_id为1700的部门
SELECT DISTINCT department_id
FROM departments
WHERE location_id = 1700
② 查询部门号=①中的任意一个的员工号
SELECT employee_id
FROM employees
WHERE department_id =ANY(
SELECT DISTINCT department_id
FROM departments
WHERE location_id = 1700
);
7.6.查询管理者是King的员工姓名和工资
① 查询姓名为king的员工编号
SELECT employee_id
FROM employees
WHERE last_name = 'K_ing'
② 查询哪个员工的manager_id = ①
SELECT last_name,salary
FROM employees
WHERE manager_id IN(
SELECT employee_id
FROM employees
WHERE last_name = 'K_ing'
);
7.7.查询工资最高的员工的姓名,要求first_name和last_name显示为一列,列名为 姓.名
① 查询最高工资
SELECT MAX(salary)
FROM employees
② 查询工资=①的姓.名
SELECT CONCAT(first_name,last_name) "姓.名"
FROM employees
WHERE salary=(
SELECT MAX(salary)
FROM employees
);
进阶8:分页查询 ★
应用场景:当要显示的数据,一页显示不全,需要分页提交sql请求
1、语法:
select 查询列表
from 表
【join type join 表2
on 连接条件
where 筛选条件
group by 分组字段
having 分组后的筛选
order by 排序的字段】
limit 【offset,】size;
offset要显示条目的起始索引(起始索引从0开始)
size 要显示的条目个数
2、特点:
①limit语句放在查询语句的最后
②公式 要显示的页数 page,每页的条目数size
select 查询列表
from 表
limit (page-1)*size,size;
size=10
page
1 0
2 10
3 20
3、案例
案例1:查询前五条员工信息
SELECT * FROM employees LIMIT 0,5;
从0开始可省略
SELECT * FROM employees LIMIT 5;
案例2:查询第11条——第25条
SELECT * FROM employees LIMIT 10,15;
案例3:有奖金的员工信息,并且工资较高的前10名显示出来
SELECT
*
FROM
employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC
LIMIT 10 ;
案例测试
已知条件:
已知表 stuinfo
id 学号
NAME 姓名
email 邮箱 john@126. com
gradeId 年级编号
sex 性别 男女
age 年龄
已知表 grade
id 年级编号,
gradeName 年级名称
求以下问题
一、查询所有学员的邮箱的用户名(注: 邮箱中@前面的字符)
二、查询男生和女生的个数
三、查询年龄>18岁的所有学生的姓名和年级名称
四、查询哪个年级的学生最小年龄>20岁
五、试说出查询语句中涉及到的所有的关键字,以及执行先后顺序
一、查询所有学员的邮箱的用户名(注: 邮箱中@前面的字符)
select substr(email,1,instr(email,'@')-1) 用户名
from stuinfo;
substr 截取字符串
instr获取字符索引
二、查询男生和女生的个数
select count(*) 个数. sex
from stuinfo
group by sex;
三、查询年龄>18岁的所有学生的姓名和年级名称
select name,gradeName
from stuinfo s
inner join grade g on s.gradeId = g.id
where age > 18;
四、查询哪个年级的学生最小年龄>20岁
1.每个年级的最小年龄
select min(age),gradeid
from stuinfo
group by gradeid
- 在1 的结果上筛选
select min(age),gradeid
from stuinfo
group by gradeid
having min(age) > 20;
五、试说出查询语句中涉及到的所有的关键字,以及执行先后顺序
右侧序号是执行顺序
select 查询列表 -------------------7
from 表1 ----------------------1
连接类型 join 表2-------------------2
on 连接条件-------------------------3
where 筛选条件----------------------4
group by 分组列表-------------------5
having 分组后的筛选------------------6
order by 排序列表-------------------8
limit 偏移(起始索引),条目数---------9
综合案例1
- 查询工资最低的员工信息: last_name, salary
- 查询平均工资最低的部门信息
- 查询平均工资最低的部门信息和该部门的平均工资
- 查询平均工资最高的 job 信息
- 查询平均工资高于公司平均工资的部门有哪些?
- 查询出公司中所有 manager 的详细信息.
- 各个部门中 最高工资中最低的那个部门的 最低工资是多少
- 查询平均工资最高的部门的 manager 的详细信息:
last_name, department_id, email, salary
1. 查询工资最低的员工信息: last_name, salary
① 查询最低的工资
SELECT MIN(salary)
FROM employees
② 查询last_name,salary,要求salary=①
SELECT last_name,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
2. 查询平均工资最低的部门信息
方式一:
① 各部门的平均工资
SELECT AVG(salary),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),department_id
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
);
④ 查询部门信息
SELECT d.*
FROM departments d
WHERE d.`department_id`=(
SELECT department_id
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
)
);
方式二:
① 各部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
② 求出最低平均工资的部门编号
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),department_id
FROM employees
GROUP BY department_id
② 求出最低平均工资的部门编号
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1;
③ 查询部门信息
SELECT d.*,ag
FROM departments d
JOIN (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
ORDER BY AVG(salary)
LIMIT 1
) ag_dep
ON d.`department_id`=ag_dep.department_id;
4. 查询平均工资最高的 job 信息
① 查询最高的job的平均工资
SELECT AVG(salary),job_id
FROM employees
GROUP BY job_id
ORDER BY AVG(salary) DESC
LIMIT 1
② 查询job信息
SELECT *
FROM jobs
WHERE job_id=(
SELECT job_id
FROM employees
GROUP BY job_id
ORDER BY AVG(salary) DESC
LIMIT 1
);
5. 查询平均工资高于公司平均工资的部门有哪些?
① 查询平均工资
SELECT AVG(salary)
FROM employees
② 查询每个部门的平均工资
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
③ 筛选②结果集,满足平均工资>①
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)>(
SELECT AVG(salary)
FROM employees
);
6. 查询出公司中所有 manager 的详细信息.
① 查询所有manager的员工编号
SELECT DISTINCT manager_id
FROM employees
② 查询详细信息,满足employee_id=①
SELECT *
FROM employees
WHERE employee_id =ANY(
SELECT DISTINCT manager_id
FROM employees
);
7. 各个部门中 最高工资中最低的那个部门的 最低工资是多少
① 查询各部门的最高工资中最低的部门编号
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY MAX(salary)
LIMIT 1
② 查询①结果的那个部门的最低工资
SELECT MIN(salary) ,department_id
FROM employees
WHERE department_id=(
SELECT department_id
FROM employees
GROUP BY department_id
ORDER BY MAX(salary)
LIMIT 1
);
8. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
① 查询平均工资最高的部门编号
SELECT
department_id
FROM
employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1
② 将employees和departments连接查询,筛选条件是①
SELECT
last_name, d.department_id, email, salary
FROM
employees e
INNER JOIN departments d
ON d.manager_id = e.employee_id
WHERE d.department_id =
(SELECT
department_id
FROM
employees
GROUP BY department_id
ORDER BY AVG(salary) DESC
LIMIT 1) ;
案例解析2
案例1:查询每个专业的学生人数
SELECT majorid,COUNT(*)
FROM student
GROUP BY majorid;
案例2:查询参加考试的学生中,每个学生的平均分、最高分
SELECT AVG(score),MAX(score),studentno
FROM result
GROUP BY studentno;
案例3:查询姓张的每个学生的最低分大于60的学号、姓名
SELECT s.studentno,s.`studentname`,MIN(score)
FROM student s
JOIN result r
ON s.`studentno`=r.`studentno`
WHERE s.`studentname` LIKE '张%'
GROUP BY s.`studentno`
HAVING MIN(score)>60;
案例4:查询每个专业生日在“1988-1-1”后的学生姓名、专业名称
SELECT m.`majorname`,s.`studentname`
FROM student s
JOIN major m
ON m.`majorid`=s.`majorid`
WHERE DATEDIFF(borndate,'1988-1-1')>0
GROUP BY m.`majorid`;
案例5:查询每个专业的男生人数和女生人数分别是多少
SELECT COUNT(*),sex,majorid
FROM student
GROUP BY sex,majorid;
案例6:查询专业和张翠山一样的学生的最低分
① 查询张翠山的专业编号
SELECT majorid
FROM student
WHERE studentname = '张翠山'
② 查询编号=①的所有学生编号
SELECT studentno
FROM student
WHERE majorid=(
SELECT majorid
FROM student
WHERE studentname = '张翠山'
)
② 查询最低分
SELECT MIN(score)
FROM result
WHERE studentno IN(
SELECT studentno
FROM student
WHERE majorid=(
SELECT majorid
FROM student
WHERE studentname = '张翠山'
)
)
案例7:查询大于60分的学生的姓名、密码、专业名
SELECT studentname,loginpwd,majorname
FROM student s
JOIN major m ON s.majorid= m.majorid
JOIN result r ON s.studentno=r.studentno
WHERE r.score>60;
案例8:按邮箱位数分组,查询每组的学生个数
SELECT COUNT(*),LENGTH(email)
FROM student
GROUP BY LENGTH(email);
案例9:查询学生名、专业名、分数
SELECT studentname,score,majorname
FROM student s
JOIN major m ON s.majorid= m.majorid
LEFT JOIN result r ON s.studentno=r.studentno
案例10:查询哪个专业没有学生,分别用左连接和右连接实现
#左
SELECT m.`majorid`,m.`majorname`,s.`studentno`
FROM major m
LEFT JOIN student s ON m.`majorid` = s.`majorid`
WHERE s.`studentno` IS NULL;
#右
SELECT m.`majorid`,m.`majorname`,s.`studentno`
FROM student s
RIGHT JOIN major m ON m.`majorid` = s.`majorid`
WHERE s.`studentno` IS NULL;
案例11:查询没有成绩的学生人数
SELECT COUNT(*)
FROM student s
LEFT JOIN result r ON s.`studentno` = r.`studentno`
WHERE r.`id` IS NULL