共计 5973 个字符,预计需要花费 15 分钟才能阅读完成。
这篇“mysql 子查询如何应用”文章的知识点大部分人都不太理解,所以丸趣 TV 小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“mysql 子查询如何应用”文章吧。
简介
子查询要包含在括号内
将子查询放在比较条件的右侧
单行操作符对应单行子查询,多行操作符对应多行子查询
分类
单行子查询:查询的结果只有 1 行
多行子查询:查询的结果有多行
相关 (或关联) 子查询:子查询中的参数需要用到主查询中的值
不相关 (或非关联) 子查询
案例 1
# 1. 查询工资比 Abel 高用户
# 方式 1:需要 2 步
SELECT salary FROM employees WHERE last_name = Abel
SELECT last_name, salary FROM employees WHERE salary 11000;
# 方式 2:自连接
SELECT e2.last_name, e2.salary
FROM employees e1, employees e2
WHERE e2.`salary` e1.`salary` # 多表的连接条件
AND e1.last_name = Abel
# 方式 3:子查询
SELECT last_name,salary
FROM employees
WHERE salary
(SELECT salary
FROM employees
WHERE last_name = Abel
);
称谓的规范
外查询(或主查询)内查询(或子查询)
单行比较操作符
代码案例
# 查询工资大于 149 号员工工资的员工的信息
SELECT employee_id, last_name, salary
FROM employees
WHERE salary (
SELECT salary
FROM employees
WHERE employee_id = 149
);
# 返回 job_id 与 141 号员工相同,salary 比 143 号员工多的员工姓名,job_id 和工资
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
);
# 返回公司工资最少的员工的 last_name,job_id 和 salary
SELECT last_name, job_id, salary
FROM employees
WHERE salary = ( SELECT MIN(salary)
FROM employees
);
# 查询与 141 号员工的 manager_id 和 department_id 相同的其他员工的 employee_id,manager_id,department_id
# 方式 1: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;
# 方式 2: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;
# 查询最低工资大于 110 号部门最低工资的部门 id 和其最低工资
SELECT department_id, MIN(salary)
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING MIN(salary) ( SELECT MIN(salary)
FROM employees
WHERE department_id = 110
);
# 显式员工的 employee_id,last_name 和 location。其中,若员工 department_id 与 location_id 为 1800 的 department_id 相同,则 location 为’Canada’,其余则为’USA’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;
# 子查询结果为 null,则最后结果为 null
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = Haas
多行子查询
也称为集合比较子查询
内查询返回多行
使用多行比较操作符
多行比较操作符
代码案例
# IN:
SELECT employee_id, last_name
FROM employees
WHERE salary IN
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
# ANY 或 ALL:
# 返回其它 job_id 中比 job_id 为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及 salary
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
);
# 返回其它 job_id 中比 job_id 为‘IT_PROG’部门所有工资低的员工的员工号、姓名、job_id 以及 salary
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
);
# 查询平均工资最低的部门 id
# MySQL 中聚合函数是不能嵌套使用的。# 方式 1:SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = ( SELECT MIN(avg_sal)
FROM( SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) t_dept_avg_sal
);
# 方式 2:SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = ALL(
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
)
# 子查询结果为 null,则最后结果为 null
SELECT last_name
FROM employees
WHERE employee_id NOT IN (
SELECT manager_id
FROM employees
);
相关子查询
子查询中使用主查询中的列
代码案例
# 查询员工中工资大于公司平均工资的员工的 last_name,salary 和其 department_id
SELECT last_name, salary, department_id
FROM employees
WHERE salary ( SELECT AVG(salary)
FROM employees
);
# 查询员工中工资大于本部门平均工资的员工的 last_name,salary 和其 department_id
# 方式 1:使用相关子查询
SELECT last_name, salary, department_id
FROM employees e1
WHERE salary ( SELECT AVG(salary)
FROM employees e2
WHERE department_id = e1.`department_id`
);
#方式 2:在 FROM 中声明子查询
SELECT e.last_name, e.salary, e.department_id
FROM employees e,( SELECT department_id,AVG(salary) avg_sal
FROM employees
GROUP BY department_id) t_dept_avg_sal # 取别名
WHERE e.department_id = t_dept_avg_sal.department_id
AND e.salary t_dept_avg_sal.avg_sal
# 查询员工的 id,salary, 按照 department_name 排序
SELECT employee_id, salary
FROM employees e
ORDER BY (
SELECT department_name
FROM departments d
WHERE e.`department_id` = d.`department_id`
) ASC;
在 SELECT 中,除了 GROUP BY 和 LIMIT 之外,其他位置都可以声明子查询
SELECT ....,....,....(存在聚合函数)
FROM ... (LEFT / RIGHT)JOIN ....ON 多表的连接条件
(LEFT / RIGHT)JOIN ... ON ....
WHERE 不包含聚合函数的过滤条件
GROUP BY ...,....
HAVING 包含聚合函数的过滤条件
ORDER BY ....,...(ASC / DESC )
LIMIT ...,....
EXISTS 与 NOT EXISTS 关键字
1、关联子查询通常也会和 EXISTS 操作符一起来使用,用来检查在子查询中是否存在满足条件的行
2、如果在子查询中不存在满足条件的行: 条件返回 FALSE
继续在子查询中查找
3、如果在子查询中存在满足条件的行: 不在子查询中继续查找
条件返回 TRUE
4、NOT EXISTS 关键字表示如果不存在某种条件,则返回 TRUE,否则返回 FALSE
代码案例
# 若 employees 表中 employee_id 与 job_history 表中 employee_id 相同的数目不小于 2,输出这些相同 id 的员工的 employee_id,last_name 和其 job_id
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`
)
# EXISTS 与 NOT EXISTS 关键字
# 查询公司管理者的 employee_id,last_name,job_id,department_id 信息
# 方式 1:自连接
SELECT DISTINCT mgr.employee_id, mgr.last_name, mgr.job_id, mgr.department_id
FROM employees emp JOIN employees mgr
ON emp.manager_id = mgr.employee_id;
# 方式 2:子查询
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE employee_id IN (
SELECT DISTINCT manager_id
FROM employees
);
#方式 3:使用 EXISTS
SELECT employee_id, last_name, job_id, department_id
FROM employees e1
WHERE EXISTS (
SELECT *
FROM employees e2
WHERE e1.`employee_id` = e2.`manager_id`
);
# 查询 departments 表中,不存在于 employees 表中的部门的 department_id 和 department_name
#方式 1:SELECT d.department_id, d.department_name
FROM employees e RIGHT JOIN departments d
ON e.`department_id` = d.`department_id`
WHERE e.`department_id` IS NULL;
# 方式 2:SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (
SELECT *
FROM employees e
WHERE d.`department_id` = e.`department_id`
);
以上就是关于“mysql 子查询如何应用”这篇文章的内容,相信大家都有了一定的了解,希望丸趣 TV 小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注丸趣 TV 行业资讯频道。
正文完