MySQL(10) 之 DQL查询(子查询)

子查询

含义:出现在其他语句中的SELECT语句,称为子查询或者内查询
      外部的查询语句称为 主查询或者外查询
分类:
    1. 按子查询的位置:
        SELECT后面:(仅仅支持标量子查询)
        FROM 后面:(支持表子查询)
        WHERE 或 HAVING 后面 :(支持标量、列 、行子查询)
        EXISTS 后面(相关子查询):(支持表子查询)
    2. 结果集的行列数不同:
        标量子查询(结果集只有一行一列)
        列子查询(结果集只有一列多行)
        行子查询(结果集只有一行多列)
        表子查询(结果集一般为多行多列)

 


        
一、WHERE HAVING 后面


1.标量子查询(单行子查询)
2.列子查询(多行子查询)
3.行子查询(多行多列)
特点:
1.子查询放在小括号内
2.子查询一般房子条件的右侧
3.标量子查询,一般搭配着单行操作符使用
> < >= <= = <>

列子查询,一般搭配着多行操作符使用
IN、ANY/SOME、ALL

1.标量子查询--案例:

1.查询工资大于abel的工资的员工名
SELECT last_name,salary
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 ,工资,条件:①和②
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;
② 查询工资最低的员工信息
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)
FROM employees
GROUP BY department_id;

③ 筛选
SELECT department_id 
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
    SELECT MIN(salary)
    FROM employees
    WHERE department_id = 50
);

 

 

2、列子查询(多行子查询)

1.返回location_id 是1400 或1700 的部门中的所有员工姓名
① 查询location_id 是1400 或者1700的部门编号
SELECT department_id
FROM departments
WHERE location_id IN (1400,1700);
② 查询员工姓名,是①中的某一个
SELECT    last_name
FROM employees
WHERE department_id IN (
    SELECT department_id
    FROM departments
    WHERE location_id IN (1400,1700)
);
或者
SELECT    last_name
FROM employees
WHERE department_id =ANY (
    SELECT 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";

② 查询比①中任意一个工资都要低的员工信息
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、行子查询

1. 查询员工编号最小并且工资最高的员工信息
先用以前的方法查询
① 查询员工编号最小的
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
);
或者 使用行子查询
SELECT *
FROM employees
WHERE (employee_id,salary) =(
	SELECT MIN(employee_id),SELECT MAX(salary)
	FROM employees
);

 

 

三 、FROM后面

# 案例:查询每个部门的平均工资的工资级别
① 查询每个部门的平均工资
SELECT AVG(salary) ,department_id
FROM employees
GROUP BY department_id;

② 连接①的结果集和job_grades表,筛选条件平均工资 在lowest_sal和hightest_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 g.lowest_sal AND g.highest_sal;

 


四、放在EXISTS后面(相关子查询)

# 是否存在,子查询结果是否有值;
SELECT EXISTS(SELECT * FROM employees);

# 查询有员工的部门名
SELECT department_name
FROM departments d
WHERE EXISTS(
	SELECT * 
	FROM employees e
	WHERE d.`department_id`=e.`department_id`
);

# 查询没有女朋友的男神信息
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS (
	SELECT boyfriend_id
	FROM beauty b
	WHERE bo.id=b.boyfriend_id
);

 

你可能感兴趣的:(MySQL,子查询)