题目:从不订单的客户
某网站包含两个表,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)算法,在元组数很多时,效率低。