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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小吴学数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值