杨大伟在路上

大数据第36天—Mysql练习题7-杨大伟

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

展示效果:

Customers
Henry
Max
 1 Create table If Not Exists Customers (Id int, Name varchar(255));
 2 Create table If Not Exists Orders (Id int, CustomerId int);
 3 
 4 insert into Customers (Id, Name) values (1, 'Joe');
 5 insert into Customers (Id, Name) values (2, 'Henry');
 6 insert into Customers (Id, Name) values (3, 'Sam');
 7 insert into Customers (Id, Name) values (4, 'Max');
 8 
 9 insert into Orders (Id, CustomerId) values (1, 3);
10 insert into Orders (Id, CustomerId) values (2, 1);

 

最终SQL:

 1 select 
 2      customers.name as 'Customers'
 3 from 
 4      customers
 5 where 
 6      customers.id not in(
 7         select
 8               customerid 
 9         from 
10               orders
11      );

 

posted on 2020-08-11 17:57  浪子逆行  阅读(104)  评论(0编辑  收藏  举报

导航