183. 从不订购的客户
一、刷题内容
原题链接
https://leetcode-cn.com/problems/customers-who-never-order/
内容描述
SQL架构
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
例如给定上述表格,你的查询应返回:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
二、解题方法
1.方法一:not in
# Write your MySQL query statement below
select
Customers.Name as Customers from Customers
where
Customers.Id not in (
select
CustomerId
from
Orders
);
2.方法二:left in
# Write your MySQL query statement below
select a.Name as Customers
from Customers as a
left join Orders as b
on
a.Id=b.CustomerId
where
b.CustomerId is null;
本文解析如何使用SQL(notin和left join方法)从Customers和Orders表中筛选出从未下单的客户,适合SQL初学者和数据库管理实践者。

被折叠的 条评论
为什么被折叠?



