【MySQL】子查询

子查询

  • 获取价格大于id为3的货物的商品
    • 用到了内查询,获取id为3的商品的单价,把结构传给外查询
  • 在where子句中编写子查询,也可以在from或select子句中编写。
use sql_store;
select *
from products
where unit_price > (
    select unit_price
    from products
    where product_id = 3
    )

运行结果:
在这里插入图片描述

  • 练习:查询工资大于平均工资的员工
use sql_hr;
select *
from employees
where salary > (
    select avg(salary)
    from employees
    )

运行结果:
在这里插入图片描述

IN运算符

  • in运算符写子查询
  • 查询没有被订购过的商品,先在子查询中找出被订购过的商品id(注意要去重)。将这个查询结果作为子查询,在外层找product_id不在子查询结果里的数据,就是没有被订购过的商品。
use sql_store;
select *
from products
where product_id not in (
    select distinct product_id
    from order_items
)

在这里插入图片描述

  • 练习:找到没有支付过支票的客户
    • 从invoices中查询去重后的clientid,将这个结果作为内查询传给外查询,找不在这个内查询结果中的id,就是没有支付过支票的顾客
use sql_invoicing;
select *
from clients
where client_id not in (
    select distinct client_id
    from invoices
)

运行结果:
在这里插入图片描述

子查询 VS 连接

  • 在运行时间差不多的情况下,应该选择最易读的查询,要注意代码的可读性!
  • 上一个练习题,可以使用外连接进行查询,但这样写可读性不好。
select *
from clients
left join invoices using (client_id)
where invoice_id is null
  • 练习
    • 找到订购了货物id为3的顾客
    • 这道题用 连接查询 思路更清晰,可读性更好
-- 用子查询写
use sql_store;
select customer_id, first_name, last_name
from customers
where customer_id in (
    select customer_id
    from order_items
    join orders using (order_id)
    where product_id = 3
)

-- 使用连接查询
select distinct customer_id, first_name, last_name
from customers
join orders using (customer_id)
join order_items using (order_id)
where product_id = 3

ALL关键字

  • 查询大于3号客户的最大发票的所有数据
use sql_invoicing;
select *
from invoices
where invoice_total > (
    select max(invoice_total)
    from invoices
    where client_id = 3
    )
  • 用all关键字
    • 查询invoice_total比all后查询到的所有数据都大的数据,一个一个的跟all后查询到的结果进行比较
select *
from invoices
where invoice_total > all(
    select invoice_total
    from invoices
    where client_id = 3
    )

返回结果
在这里插入图片描述

  • max写法和all写法可以相互改写,两种写法的可读性都较好

ANY关键字

  • in 和 = any是等价的。
  • 查询至少有两张发票的客户id
    • 使用count(*)查到所有的信息,根据client_id分组,分组后用having进行条件筛选
select client_id, count(*)
from invoices
group by client_id
having count(*) >= 2
  • 把上述查询当子查询,把clients中至少有两张发票的客户信息查出来
    • where子句中可以用in,也可以用 = any
    • in 和 = any的效果是一样的,用哪种都行
-- in
select *
from clients
where client_id in (
    select client_id
    from invoices
    group by client_id
    having count(*) >= 2
)
-- = any
select *
from clients
where client_id = any (
    select client_id
    from invoices
    group by client_id
    having count(*) >= 2
)

相关子查询 !

  • 查询逻辑:先到employees表,对每个员工e执行这段子查询,计算和e同一个部门的员工的平均工资,如果这名员工e的工资高于平均工资,就会被返回在结果中。依次一条一条的去查询。
  • 这种查询成为相关子查询,子查询和外查询存在相关性,引用了外查询里出现的别名(即e)
  • 使用相关子查询时,这段子查询会在主查询每一行的层面执行,所以相关子查询经常执行的很慢。
use sql_hr;
select *
from employees e
where salary > (
    select avg(salary)
    from employees
    where office_id = e.office_id
    )
  • 练习
    • 查询顾客大于自己平均值的数据
use sql_invoicing;
select *
from invoices i
where invoice_total > (
    select avg(invoice_total)
    from invoices
    where client_id = i.client_id
    )

EXISTS运算符

  • 获取在发票表中有发票的客户
    • 三种写法:子查询,外连接,exists相关子查询
  • 用in,先将in后的子查询运行结果返回给where。
  • in后的子查询会生成一个列表,返回给where。如果子查询查到的过多,会导致列表特别大,这样会妨碍最佳性能;对于这种情况,用exists能提高效率
select *
from clients
where client_id in (
    select distinct client_id
    from invoices
    )
  • 用exists运算符,来查看发票表里是否存在符合这个条件的行
  • 子查询并没有给外查询返回一个结果,它会返回一个指令,说明这个子查询中是否有符合这个搜索条件的行,每一行外层查询的数据都到exists后去看是否存在;如果存在,子查询就会给exists返回true,exists运算符就会在最终结果里添加当前的记录。
select *
from clients
where exists(
    select client_id
    from invoices
    where invoices.client_id = clients.client_id
);

运行结果
在这里插入图片描述

  • 练习
    • 找到从没有被订购过的商品
use sql_store;
select *
from products
where not exists(
    select product_id
    from order_items
    where order_items.product_id = products.product_id
)

运行结果
在这里插入图片描述

select子句中的子查询

  • 在select子句中用子查询得到平均值
  • select语句中 在表达式中不能使用列的别名,这样就只能把select子句中的子查询再复制一遍,但是这样很长很麻烦且重复;解决方法是:再转换成一个子查询(select invoice_average)
use sql_invoicing;
select invoice_id,
       invoice_total,
       (select avg(invoice_total)
            from invoices) as invoice_avearge,
       invoice_total - (select invoice_avearge) as difference
from invoices

运行结果
在这里插入图片描述

  • 练习:得到每个客户的总发票金额,全部发票的平均值,以及他们的差值
select client_id,
       name,
       (select sum(invoice_total)
        from invoices
        where client_id = c.client_id) as total_sales,
       (select avg(invoice_total)
        from invoices) as average,
       (select total_sales) - (select average) as difference
from clients c

运行结果
在这里插入图片描述

from子句中的子查询

  • 可以把一段查询生成的表当作另一个查询的from
select *
from(
select client_id,
       name,
       (select sum(invoice_total)
        from invoices
        where client_id = c.client_id) as total_sales,
       (select avg(invoice_total)
        from invoices) as average,
       (select total_sales) - (select average) as difference
from clients c
) as hahah
where total_sales is not null

运行结果
在这里插入图片描述

  • 46
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuwuuu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值