leetcode-183-Customers-Who-Never-Order

描述


Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.

1
2
3
4
5
6
7
8
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+

Table: Orders.

1
2
3
4
5
6
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+

Using the above tables as example, return the following:

1
2
3
4
5
6
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+

分析


两张表,一张 Customer,一张 Orders,通过这两张表找出没有订购过商品的顾客。

解决方案(MySQL)


1
2
3
4
5
6
7
8
# Write your MySQL query statement below
SELECT
C.Name as Customers
FROM Customers C
WHERE C.Id not in
(
SELECT CustomerId FROM Orders
)

题目来源