Skip to content

Commit 22b2294

Browse files
committed
Add solution 183 [sql]
left join
1 parent 8740e54 commit 22b2294

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
3838
| 176 | [Second Highest Salary](https://github.com/doocs/leetcode/tree/master/solution/176.Second%20Highest%20Salary) | `SQL` |
3939
| 181 | [Employees Earning More Than Their Managers](https://github.com/doocs/leetcode/tree/master/solution/181.Employees%20Earning%20More%20Than%20Their%20Managers) | `SQL` |
4040
| 182 | [Duplicate Emails](https://github.com/doocs/leetcode/tree/master/solution/182.Duplicate%20Emails) | `SQL` |
41+
| 183 | [Customers Who Never Order](https://github.com/doocs/leetcode/tree/master/solution/183.Customers%20Who%20Never%20Order) | `SQL` |
4142
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
4243
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
4344
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 从不订购的客户
2+
### 题目描述
3+
4+
某网站包含两个表,`Customers` 表和 `Orders` 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
5+
6+
`Customers` 表:
7+
```
8+
+----+-------+
9+
| Id | Name |
10+
+----+-------+
11+
| 1 | Joe |
12+
| 2 | Henry |
13+
| 3 | Sam |
14+
| 4 | Max |
15+
+----+-------+
16+
```
17+
18+
`Orders` 表:
19+
```
20+
+----+------------+
21+
| Id | CustomerId |
22+
+----+------------+
23+
| 1 | 3 |
24+
| 2 | 1 |
25+
+----+------------+
26+
```
27+
28+
例如给定上述表格,你的查询应返回:
29+
```
30+
+-----------+
31+
| Customers |
32+
+-----------+
33+
| Henry |
34+
| Max |
35+
+-----------+
36+
```
37+
38+
### 解法
39+
两个表左连接,找出 `CustomerId``null` 的记录即可。
40+
41+
```sql
42+
# Write your MySQL query statement below
43+
44+
select Name as Customers
45+
from Customers c
46+
left join Orders o
47+
on c.Id = o.CustomerId
48+
where o.CustomerId is null
49+
```
50+
51+
#### Input
52+
```json
53+
{"headers": {"Customers": ["Id", "Name"], "Orders": ["Id", "CustomerId"]}, "rows": {"Customers": [[1, "Joe"], [2, "Henry"], [3, "Sam"], [4, "Max"]], "Orders": [[1, 3], [2, 1]]}}
54+
```
55+
56+
#### Output
57+
```json
58+
{"headers":["Customers"],"values":[["Henry"],["Max"]]}
59+
```

0 commit comments

Comments
 (0)