左连接保证左表结果一定查出,关联右表数据null时,对应右列null
select c.name as `Customers`
from Customers c
left join Orders o
on (c.id=o.customerId)
where o.id is null;
join查出的是有下单的顾客数据,not in进行反选
select name as `Customers`
from Customers
where id not in (
select c.id
from Customers c
join Orders o
on (c.id=o.customerId)
);
内部子查询可简化为单表查:
select name as `Customers`
from Customers
where id not in (
select customerId
from Orders
);