LeetCode 183. 从不订购的客户【mysql】
183. 从不订购的客户
题意:
某网站包含两个表,
Customers
表和Orders
表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
例如给定上述表格,你的查询应返回:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
Code:
# 方法一:通过子查询和in
select name as Customers from customers where id not in(
select customerid from orders
);
# 方法二:通过left join 将其连成一个表
select Name as Customers from customers as a left join orders as b on
a.id = b.customerid where b.id is null;
还没有评论,来说两句吧...