从不订购的客户

题目:从不订单的客户
  某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

Customers 表:

±—±------+
| Id | Name |
±—±------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
±—±------+
Orders 表:

±—±-----------+
| Id | CustomerId |
±—±-----------+
| 1 | 3 |
| 2 | 1 |
±—±-----------+
例如给定上述表格,你的查询应返回:

±----------+
| Customers |
±----------+
| Henry |
| Max |
±----------+

法一:左连接,七种连接理论的 一张表有该元组另一种表没有的该元组的情况,差集。
  1.按左外连接left join将表连接,左表为Customers,右表为Orders;
  2.之后where选择出新表中 代表右表的字段中字段值为null的元组;
  3.用select投影到选择出的元组的Name属性。

select c.Name as Customers 
from Customers c left join Orders o 
on c.Id=o.CustomerId
where o.CustomerId is null;

注:七种连接理论

法二:用不相关子查询+not in
  1.查询订单表,因为是不相关子查询,所以先执行子查询,把Orders里所以的客户编号找到;
  2.查询客户表,用where选择出编号不在订单表的用户元组;
  3.select投影到选择出的元组的Name字段。

select Name as Customers
from Customers 
where Id not in(
    select CustomerId 
    from Orders
);

法三:相关子查询+not exists
  1.把客户表的编号一个一个的拿到子查询里遍历订单表的每一个元组,之后子查询里找不到匹配的订单元组则not exists返回true给where,否则返回false;
  2.not exists 就是该元组不存在则返回true,where选择成立。

select Name as Customers
from Customers c
where not exists(
    select *
    from Orders o
    where o.CustomerId=c.Id
);

注:相关子查询的执行过程是一个双重循环的过程,类似排序算法里的O(n^2)算法,在元组数很多时,效率低。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值