vlambda博客
学习文章列表

06-mysql基础-mysql中的DQL-子查询

点击蓝色“潭时录”关注我丫

每天和小潭一起快乐的学习~

    你好,我是在学mysql的小潭。在上一期的推文中我们学习了mysql的连接查询知识,本期我们将继续学习DQL中的子查询。


子查询介绍

含义:出现在其他语句中的select语句,成为子查询或内查询外部的查询语句,成为主查询或外查询
分类:按子查询出现的位置: select后面 仅支持标量子查询 from后面 支持表子查询 wherehaving后面 支持标量子查询(*) 列子查询(*) 行子查询 exists后面(相关子查询) 表子查询按结果集的行列数不同: 标量子查询(结果集只有一行一列) 列子查询(结果集有一列多行) 行子查询(结果集有一行多列) 表子查询(结果集一般为多行多列)

其中where或having是常用到,其后可以是以下类型:

1.标量子查询(单行子查询)2.列子查询(多行子查询)3.行子查询(允许多行多列)
特点:1.子查询放在小括号内2.子查询一般放在条件的右侧3.标量子查询,一般搭配单行操作符使用(>,<,>=,<=,=,<>条件运算符) 列子查询,一般搭配多行操作符使用(IN/NOT IN,ANY/SOME,ALL) 区别举例: a>any(10,20,30)等同于a>min(10,20,30) a>all(10,20,30)等同于a>max(10,20,30)4.子查询的执行优先于主查询的执行,主查询的条件用到了子查询的结果


具体示例:

  • 标量子查询(where或having后)

#案例1:谁的工资比Abel高?/*思路:1.查询Abel的工资 2.查询员工的信息,满足salary>1的结果*/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 2.查询143号员工的salary 3.查询姓名,job_id和工资,要求job_id=1的结果并且salary>2的结果*/SELECT last_name,job_id,salaryFROM employeesWHERE 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.查询公司员工的最低工资 2.查询last_name,job_id,salary,要求salary=1的结果*/SELECT last_name,job_id,salaryFROM employeesWHERE salary = ( SELECT MIN(salary) FROM employees);
##############################################################################################################################
#标量子查询(having后)#案例4:查询最低工资大于50号部门最低工资的部门id和其最低工资/*思路:1.查询50号部门的最低工资 2.查询每个部门的最低工资 3.筛选2的结果中min(salary)>1的结果*/SELECT department_id,MIN(salary)FROM employeesGROUP BY department_idHAVING MIN(salary) >( SELECT MIN(salary) FROM employees WHERE department_id = 50);

非法使用标量子查询的情况:

#1.子查询结果量大于1SELECT department_id,MIN(salary)FROM employeesGROUP BY department_idHAVING MIN(salary) >( SELECT salary FROM employees WHERE department_id = 50);#2.子查询结果为空SELECT department_id,MIN(salary)FROM employeesGROUP BY department_idHAVING MIN(salary) >( SELECT MIN(salary) FROM employees WHERE department_id = 250);


  • 列子查询(多行子查询)

#案例1:返回location_id是1400或1700的部门中所有员工姓名/*思路:1.查询location是1400或1700的部门编号 2.查询员工姓名,要求部门号是1中的某一个*/SELECT last_nameFROM employeesWHERE department_id IN #此处in可以换做=ANY / 如果是not in可以换做<>ALL( SELECT DISTINCT department_id FROM departments WHERE location_id IN (1400,1700));
#案例2:返回其他工种中比job_id为'IT_PROG'工种任一工资低的员工的工号,姓名,job_id和salary/*思路:1.查询job_id为'IT_PROG'部门任一工资 2.查询员工号,姓名,job_id和salary,要求salary<1的结果*/SELECT employee_id,last_name,job_id,salaryFROM employeesWHERE salary <ANY(SELECT salary FROM employeesWHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG';#或SELECT employee_id,last_name,job_id,salaryFROM employeesWHERE salary <(SELECT MAX(salary) FROM employeesWHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG';
#案例3:返回其他工种中比job_id为'IT_PROG'工种所有工资都低的员工的员工号,姓名,job_id和salarySELECT employee_id,last_name,job_id,salaryFROM employeesWHERE salary <ALL(SELECT salary FROM employeesWHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG';#或SELECT employee_id,last_name,job_id,salaryFROM employeesWHERE salary <(SELECT MIN(salary) FROM employeesWHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG';


  • 行子查询

#案例1:查询员工编号最小并且工资最高的员工信息/*传统思路:1.查询最小的员工编号 2.查询最高工资 3.查询员工信息*/SELECT *FROM employeesWHERE employee_id = ( SELECT MIN(employee_id) FROM employees) AND salary =( SELECT MAX(salary) FROM employees);#使用行子查询代替SELECT * FROM employeesWHERE (employee_id,salary) =( SELECT MIN(employee_id),MAX(salary) FROM employees);


  • 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) 部门名;


  • from后的子查询

/*将子查询结果充当一张表,要求必须起别名*/#案例1:查询每个部门的平均工资的工资等级/*思路:1.查询每个部门的平均工资 2.连接1的结果集和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_depINNER JOIN job_grades gON ag_dep.ag BETWEEN lowest_sal AND highest_sal;


  • exists后的子查询(相关子查询)

/*语法:exists(完整的查询语句)结果:1或0*/#案例1:查询有员工的部门名#inSELECT department_nameFROM departments dWHERE d.`department_id` IN ( SELECT department_id FROM employees);#exists用法SELECT department_nameFROM departments dWHERE EXISTS( SELECT * FROM employees e WHERE d.`department_id`=e.`department_id`);


小测试:

#查询和Zlotkey相同部门的员工姓名和工资SELECT last_name,salaryFROM employeesWHERE department_id = ( SELECT department_id FROM employees WHERE last_name = 'Zlotkey');
#查询工资比公司平均工资高的员工的员工号,姓名和工资SELECT employee_id,last_name,salaryFROM employeesWHERE salary >( SELECT AVG(salary) FROM employees);
#查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资/*思路:1.查询各部门的平均工资 2.连接1结果集和employees表,再添加筛选*/SELECT employee_id,last_name,salary,e.`department_id`FROM employees eINNER JOIN( SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id) ag_depON e.`department_id`=ag_dep.department_idWHERE salary>ag_dep.ag;
#查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名/*思路:1.查询姓名中包含字母u的员工的部门 2.查询部门号=1中的任意一个的员工号和姓名*/SELECT employee_id,last_nameFROM employeesWHERE department_id IN( SELECT DISTINCT department_id FROM employees WHERE last_name LIKE '%u%');
#查询在部门的location_id为1700的部门工作的员工的员工号/*思路:1.查询locaion_id为1700的部门 2.查询部门号=1中任意一个的员工号*/SELECT employee_idFROM employeesWHERE department_id IN #或使用=ANY( SELECT DISTINCT department_id FROM departments WHERE location_id = 1700);
#查询管理者是K_ing的员工的姓名和工资/*思路:1.查询姓名为K_ing的员工编号 2.查询哪个员工的manager_id=1的结果*/SELECT last_name,salaryFROM employeesWHERE manager_id IN #当不确定子查询结果有多少个时,可以使用IN( SELECT employee_id FROM employees WHERE last_name='K_ing');
#查询工资最高的员工的姓名,要求first_name和last_name显示为一列,姓名为姓.名/*思路:1.查询最高工资 2.查询工资=1结果的姓名*/SELECT CONCAT(first_name,'.',last_name) "姓.名"FROM employeesWHERE salary IN( SELECT MAX(salary) FROM employees);









下期预告:mysql中的查询4




如果你喜欢本文,
请长按二维码,关注 潭时录 .
转发至朋友圈,是对我最大的支持。

点个 在看 
喜欢是一种感觉
在看是一种支持
↘↘↘