183.找出所有从不点任何东西的顾客
建表插入数据:
#插入数据,顾客表和订单表
Create table If Not Exists Customers (id int, name varchar(255))
Create table If Not Exists Orders (id int, customerId int)
Truncate table Customers
insert into Customers (id, name) values ('1', 'Joe')
insert into Customers (id, name) values ('2', 'Henry')
insert into Customers (id, name) values ('3', 'Sam')
insert into Customers (id, name) values ('4', 'Max')
Truncate table Orders
insert into Orders (id, customerId) values ('1', '3')
insert into Orders (id, customerId) values ('2', '1')
思路分析:
顾客表的id等于订单表的顾客id,所以建立左连接,可以看出哪些顾客没有点东西
画图分析:
代码实现:
#最终代码
select c.name as Customers from customers c left join orders o on c.id=o.customerId
where o.id is null;