08-子查询
Posted 坤坤就爱露露
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了08-子查询相关的知识,希望对你有一定的参考价值。
1、说明:
当我们的一个查询语句中有嵌套了一个完整的select语句,
被嵌套的select语句称为子查询或内查询,外面的select语句称为主查询或外查询
2、分类:
按子查询出现的位置进行分类
1.select后面
要求:子查询的结果为单行单列(标量子查询)
2.from后面
要求:子查询的结果可以为多行多列
3、where或having后面 ★
要求:子查询的结果必须为 单列
单行子查询
多行子查询
4、exists后面
要求:子查询结果必须为单列(相关子查询)
3、特点:
1、子查询放在条件中,要求必须放在条件的右侧
2、子查询一般放在小括号中
3、子查询的执行优先于主查询
4、单行子查询对应了 单行操作符:> < >= <= = <>
多行子查询对应了 多行操作符:any/some all in
5、可以替换的单行和多行操作符in =(=any),not in=(<>all)
一、where或having后面 ★
#标量子查询(就是单行子查询)
案例一:谁的工资比abel高
#1.查询Able的工资
SELECT salary
FROM employees
WHERE last_name
=‘Abel’;
#2.查询员工信息满足salary>第一步查询的结果
SELECT
- FROM
employees
WHEREsalary
>
(SELECT*
salary
FROM
employees
WHERElast_name
= ‘Abel’) ;
案例2:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资
#1.查询141的job_id
SELECT
job_id
FROM
employees
WHERE employee_id
= 141 ;
#2.查询143的工资
SELECT
salary
FROM
employees
WHERE employee_id
= 143 ;
#3.进行筛选
SELECT
job_id
,
last_name
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
#1.查询最低工资
SELECT MIN(salary
)
FROM employees
;
#2.查询最低工资等1查询结果的员工
SELECT last_name
,job_id
,salary
FROM employees
WHERE salary
=(SELECT MIN(salary
)
FROM employees
);
二、列子查询与多行操作符:
列子查询(多行子查询)
需要搭配多行比较操作符 IN ,NOT IN,ANY,SOME,ALL
in:判断某字段是否在指定列表内
X IN(10,30,50)
ANY/SOME:判断某字段的值是否满足其中任意一个
X>ANY(10,30,50)
X>MIN()
X=ANY(10,30,50)
X IN(10,30,50)
ALL:判断某字段的值是否满足里面所有的
X >ALL(10,30,50)
X >MAX()
案例1:返回location_id是1400或1700的部门中的所有员工姓名
#1.第一步查询出location_id是1400或1700的部门编号
SELECT DISTINCT department_id
FROM departments
WHERE location_id
IN (1400,1700);
#2.在employees表中过滤出部门编号相同的员工
SELECT
last_name
,
department_id
FROM
employees
WHERE department_id
IN
(SELECT DISTINCT
department_id
FROM
departments
WHERE location_id
IN (1400, 1700)
) ;
题目:返回其它部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
#第一步查询job_id为’IT_PROG’的部门
SELECT DISTINCT salary
FROM employees
WHERE job_id
=‘IT_PROG’;
#第二步输出结果集
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
);
三、放在select后面
#案例:查询每个部门的员工个数
SELECT d.,(
SELECT COUNT()
FROM employees
e
WHERE d.department_id
=e.department_id
) 个数
FROM departments
d;
四、放在from后面
将子查询出的虚拟表当做一个表进行连接输出
#案例1:查询每个部门的平均工资的工资级别
#1.先查询部门的平均工资
SELECT AVG(salary
),department_id
FROM employees
GROUP BY department_id
;
#2.将第一步查询出的结果表放在from后面,连接查询的的虚拟表
SELECT
dep_ag.department_id
,
dep_ag.ag,
g.grade_level
FROM
job_grades
g
JOIN
(SELECT
AVG(salary
) ag,
department_id
FROM
employees
GROUP BY department_id
) dep_ag
ON dep_ag.ag BETWEEN g.lowest_sal
AND g.highest_sal
;
五、放在exists后面
#案例1:查询有无明仔叫张三丰的员工信息
SELECT *
FROM employees
WHERE last_name=‘张三丰’;
当表位空时就会输出0,不为空时就会输出1,借此我们可以判断是否存在这个人
SELECT EXISTS(
SELECT *
FROM employees
WHERE last_name=‘张三丰’
) 有无此人;
#一、where或having后面 ★
#标量子查询(就是单行子查询)
#案例一:谁的工资比abel高
#1.查询Able的工资
SELECT `salary`
FROM `employees`
WHERE `last_name`='Abel';
#2.查询员工信息满足salary>第一步查询的结果
SELECT
*
FROM
`employees`
WHERE `salary` >
(SELECT
`salary`
FROM
`employees`
WHERE `last_name` = 'Abel') ;
#案例2:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资
#1.查询141的job_id
SELECT
`job_id`
FROM
`employees`
WHERE `employee_id` = 141 ;
#2.查询143的工资
SELECT
`salary`
FROM
`employees`
WHERE `employee_id` = 143 ;
#3.进行筛选
SELECT
`job_id`,
`last_name`
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
#1.查询最低工资
SELECT MIN(`salary`)
FROM `employees`;
#2.查询最低工资等1查询结果的员工
SELECT `last_name`,`job_id`,`salary`
FROM `employees`
WHERE `salary`=(SELECT MIN(`salary`)
FROM `employees`);
#列子查询(多行子查询)
需要搭配多行比较操作符 IN ,NOT IN,ANY,SOME,ALL
#in:判断某字段是否在指定列表内
X IN(10,30,50)
ANY/SOME:判断某字段的值是否满足其中任意一个
X>ANY(10,30,50)
X>MIN()
X=ANY(10,30,50)
X IN(10,30,50)
ALL:判断某字段的值是否满足里面所有的
X >ALL(10,30,50)
X >MAX()
#案例1:返回location_id是1400或1700的部门中的所有员工姓名
#第一步查询出location_id是1400或1700的部门编号
SELECT DISTINCT `department_id`
FROM `departments`
WHERE `location_id`IN (1400,1700);
#2.在employees表中过滤出部门编号相同的员工
SELECT
`last_name`,
`department_id`
FROM
`employees`
WHERE `department_id` IN
(SELECT DISTINCT
`department_id`
FROM
`departments`
WHERE `location_id` IN (1400, 1700)
) ;
#题目:返回其它部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary
#第一步查询job_id为'IT_PROG'的部门
SELECT DISTINCT `salary`
FROM `employees`
WHERE `job_id`='IT_PROG';
#第二步输出结果集
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
);
#行子查询(多列多行查询)
#二、放在select后面
#案例:查询每个部门的员工个数
SELECT d.*,(
SELECT COUNT(*)
FROM `employees` e
WHERE d.`department_id`=e.`department_id`
) 个数
FROM `departments` d;
#三、放在from后面
#案例1:查询每个部门的平均工资的工资级别
#1.先查询部门的平均工资
SELECT AVG(`salary`),`department_id`
FROM `employees`
GROUP BY `department_id`;
#2.将第一步查询出的结果表放在from后面,连接查询的的虚拟表
SELECT
dep_ag.`department_id`,
dep_ag.ag,
g.`grade_level`
FROM
`job_grades` g
JOIN
(SELECT
AVG(`salary`) ag,
`department_id`
FROM
`employees`
GROUP BY `department_id`) dep_ag
ON dep_ag.ag BETWEEN g.`lowest_sal`
AND g.`highest_sal` ;
#四、放在exists后面
#案例1:查询有无明仔叫张三丰的员工信息
SELECT *
FROM `employees`
WHERE last_name='张三丰';
SELECT EXISTS(
SELECT *
FROM `employees`
WHERE last_name='张三丰'
) 有无此人;
以上是关于08-子查询的主要内容,如果未能解决你的问题,请参考以下文章
中继fetchQuery:如何在没有片段结构的情况下获取查询结果