大数据第35天—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      );

 

上一篇:OCP考试专项 [1z0-071]-Q6: implicit convertion 隐式转换(2020.06.16)


下一篇:SQL 语法