Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18 从不订购的客户 #23

Open
astak16 opened this issue Jan 12, 2022 · 0 comments
Open

18 从不订购的客户 #23

astak16 opened this issue Jan 12, 2022 · 0 comments
Labels

Comments

@astak16
Copy link
Owner

astak16 commented Jan 12, 2022

题目

找出所有从不订购任何东西的客户

create table customers (
	id int primary key auto_increment,
	name varchar(255)
);
insert into customers (name) values('Joe'), ('Henry'), ('Sam'), ('Max');

create table orders (
	id int primary key auto_increment,
	customerId int
);
insert into orders (customerId) values(3),(1);

SQL:方法一

select name from customers left join orders
on customers.id = orders.customerId where isnull(customerId);

解析

使用 left join 连接 customersorders 连接条件是 customers.id = orders.customersId and isnull(customersId)

SQL:方法二

select name from customers where id not in (
	select customerId from orders
);

解析

使用 not in 查出不在这些结果中的数据。

SQL:方法三

select name from customers where not exists (
	select customerId from orders where customerId = customers.id
);

解析

使用 not exists 代替 not in

@astak16 astak16 added the 简单 label Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant