MySQL之子查询

    1.子查询语法
        ·子查询(内查询)在主查询之前一次执行完成
        ·子查询的结果被主查询(外查询)使用
    2.子查询类型
        ·单行子查询
            ·只返回一行
            ·使用单行比较操作符
                "=":Equal to
                ">":Greater than
                ">=":Greater than or equal to
                "<":Less than
                "<=":Less than or equal to
                "<>":Not equal to 
            执行单行子查询:
                题目:返回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
            );


            子查询中的HAVING子句
                ·首先执行子查询
                ·向主查询中的HAVING子句返回结果
                题目:查询最低工资大于50号部门最低工资的部门id和其最低工资

select department_id,
       min(salary)
from employees
group by department_id
having min(salary) >
                (select min(salary)
                 from employees
                 where department_id = 50
                );

             子查询中的空值问题

select last_name,
       job_id
from employees
where job_id = 
            (select job_id
             from employees
             where last_name = 'Haas'
            );

no rows selected

                         子查询不返回任何行

             非法使用子查询

                        

select employee_id,
       last_name
from employees
where salary = 
            (select min(salary)
             from employees
             group by department_id);

错误代码:1242

Subquery returns more than 1 row

                         多行子查询使用单行比较符

         ·多行子查询

                ·返回多行

                ·使用多行比较操作符

                        "IN":等于列表中的任意一个

                        "ANY":和子查询返回的某一个值比较

                        "ALL":和子查询返回的所有值比较

                                多行子查询中使用ANY操作符

                                        题目:返回其他部门中比job_id为'IT_PROG'部门任一工资低的员工的员工号、姓名、job_id以及salary

select employee_id,
       last_name,
       job_id,
       salary
from employees
where salary < any
                (select salary
                 from employees
                 where job_id = 'IT_PROG'
                )
and job_id <> 'IT_PROG';

                                多行子查询中使用ALL操作符

                                        题目:返回其他部门中比job_id为'IT_PROG'部门所有工资都低的员工的员工号、姓名、job_id以及salary

select employee_id,
       last_name,
       job_id,
       salary
from employees
where salary < all
                (select salary
                 from employees
                 where job_id = 'IT_PROG'
                )
and job_id <> 'IT_PROG';

                子查询中的空值问题

select emp.last_name
from employees emp
where emp.employee_id not in
                        (select mgr.manager_id
                         from employees mgr
                        );

no rows selected

你可能感兴趣的:(MySQL子查询,java,开发语言)