MySQL实例讲解子查询的使用
瀛台夜雪 人气:0子查询-嵌套查询
子查询是指一个查询语句嵌套在另一个语句内部的查询
原始查询方法
SELECT last_name,salaryFROM employeesWHERE last_name='Abel';SELECT last_name,salaryFROM employeesWHERE salary>11000;
自连接
SELECT e2.last_name,e2.salaryFROM employees e1,employees e2WHERE e2.salary>e1.salary AND e1.last_name='Abel';
子查询
SELECT last_name,salaryFROM employeesWHERE salary>(SELECT salaryFROM employeesWHERE last_name='Abel');
称谓的规范:外查询(或主查询),内查询
- 子查询(内查询)在主查询之前一次执行完成。
- 子查询的结果被主查询(外查询)使用 。
注意事项
- 子查询要包含在括号内
- 将子查询放在比较条件的右侧
- 单行操作符对应单行子查询,多行操作符对应多行子查询
子查询分类
单行子查询 vs 多行子查询
- 按内查询的结果返回一条还是多条记录,将子查询分为单行子查询和多行子查询
相关子查询vs不相关子查询
- 按照内查询是否被执行多次进行划分
- 相关子查询需求:查询工资大于本部分平均工资的员工信息
- 不相关子查询需求:查询工资大于本公司平均工资的员工信息
单行子查询
单行比较运算符
操作符 | 含义 |
---|---|
= | equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
<> | not equal to |
子查询的编写思路
- 从里往外写
- 从外往里写
SELECT * FROM employees WHERE salary>( SELECT salary FROM employees WHERE employee_id=149 ); 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 ); SELECT employee_id,manager_id,department_id FROM employees WHERE manager_id=( SELECT manager_id FROM employees WHERE employee_id=141 ) AND department_id=( SELECT department_id FROM employees WHERE employee_id=141 ) AND employee_id<>141; SELECT employee_id,manager_id,department_id FROM employees WHERE (manager_id,department_id)=( SELECT manager_id,department_id FROM employees WHERE employee_id=141 ) AND employee_id<>141;
HAVING中的子查询
- 先执行子查询
- 向主查询中的HAVING字句返回结果
SELECT department_id,MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary)>( SELECT MIN(salary) FROM employees WHERE department_id=50 );
CASE中的子查询
在CASE表达式中使用单列子查询
SELECT employee_id,last_name ,CASE department_id WHEN (SELECT department_id FROM departments WHERE location_id=1800) THEN 'Canada' ELSE 'USA' END 'location' FROM employees;
子查询中的空值问题
若内查询查询结果为空的,则不会报错,子查询不会返回任何行
非法使用子查询
多行子查询
多行子查询也被称为集合比较子查询
内查询返回多行数据
使用多行比较操作符
多行比较操作符
操作符 | 含义 |
---|---|
IN | 等于列表中的任意一个 |
ANY | 需要和单行比较操作符一起使用,和子查询返回的某一个值比较(任一) |
ALL | 需要和单行比较操作符一起使用,和子查询返回的所有值比较 |
SOME | 实际上是ANY的别名,作用相同,一般常使用ANY |
MySQL中聚合函数是不能嵌套的
SELECT employee_id,last_name FROM employees WHERE salary IN ( SELECT MIN(salary) FROM employees GROUP BY department_id ); SELECT employee_id,last_name,job_id,salary FROM employees WHERE job_id<>'IT_PROG' AND salary < ANY ( SELECT salary FROM employees WHERE job_id='IT_PROG' ); SELECT employee_id,last_name,job_id,salary FROM employees WHERE job_id<>'IT_PROG' AND salary < ALL ( SELECT salary FROM employees WHERE job_id='IT_PROG' ); SELECT department_id FROM employees GROUP BY department_id HAVING AVG(salary) <= ALL ( SELECT AVG(salary) avgsal FROM employees GROUP BY department_id ); SELECT department_id FROM employees GROUP BY department_id HAVING AVG(salary)= ( SELECT MIN(avgsal) FROM ( SELECT AVG(salary) avgsal FROM employees GROUP BY department_id ) dbsal );
多行子查询空值问题
子查询中存在空值的情况将使得查询结果生成的位空
相关子查询
如果子查询的执行依赖于外部查询,通常情况下是因为子查询中的表用到了外部的表,并进行了条件关联,因此每职系那个一次外部查询,子查询都需要重新计算一次,这样的子查询便被称之为关联子查询
相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询
- GET 从主查询中获取候选列
- EXECUTE 子查询使用主查询的数据
- USE 如果满足子查询的条件则返回该行
- 再返回1
SELECT last_name,salary,department_id FROM employees e1 WHERE salary > ( SELECT AVG(salary) FROM employees e2 WHERE department_id=e1.department_id ); #在from中进行子查询 SELECT e1.last_name,e1.salary,e1.department_id FROM employees e1, ( SELECT department_id ,AVG(salary) avgs FROM employees GROUP BY department_id ) t1 WHERE e1.department_id=t1.depa SELECT employee_id,salary FROM employees e ORDER BY( SELECT department_name FROM departments d WHERE e.department_id=d.department_id ) ASC; SELECT employee_id,last_name,job_id FROM employees e WHERE 2<= ( SELECT COUNT(*) FROM job_history j WHERE e.employee_id=j.employee_id )
结论:在SELECT中,除了GROUP BY和LIMIT之外,其他位置都可以声明子查询
EXISTS与NOT EXISTS 关键字
关联子查询通常也会和EXISTS操作符一起来使用,用来检查在子查询中是否存在满足条件的行
如果在子查询中不存在满足条件的行
- 条件返回FALSE
- 继续在子查询中查找
如果在子查询中存在满足条件的行
- 不在子查询中继续查找
- 条件返回TRUE
NOT EXISTS关键字表示不存在某种条件,则返回TRUE,否则返回FALSE
加载全部内容