Skip to content

Commit 8520ae2

Browse files
committed
Add solution 175 [sql]
1 parent 9e27d9d commit 8520ae2

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
3434
| 136 | [Single Number](https://github.com/doocs/leetcode/tree/master/solution/136.Single%20Number) | `Hash Table`, `Bit Manipulation` |
3535
| 141 | [Linked List Cycle](https://github.com/doocs/leetcode/tree/master/solution/141.Linked%20List%20Cycle) | `Linked List`, `Two Pointers` |
3636
| 155 | [Min Stack](https://github.com/doocs/leetcode/tree/master/solution/155.Min%20Stack) | `Stack`, `Design` |
37+
| 175 | [Combine Two Tables](https://github.com/doocs/leetcode/tree/master/solution/175.Combine%20Two%20Tables) | `SQL` |
3738
| 189 | [Rotate Array](https://github.com/doocs/leetcode/tree/master/solution/189.Rotate%20Array) | `Array` |
3839
| 198 | [House Robber](https://github.com/doocs/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
3940
| 203 | [Remove Linked List Elements](https://github.com/doocs/leetcode/tree/master/solution/203.Remove%20Linked%20List%20Elements) | `Linked List` |
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
## 组合两个表
2+
### 题目描述
3+
4+
表1: `Person`
5+
```
6+
+-------------+---------+
7+
| 列名 | 类型 |
8+
+-------------+---------+
9+
| PersonId | int |
10+
| FirstName | varchar |
11+
| LastName | varchar |
12+
+-------------+---------+
13+
PersonId 是上表主键
14+
```
15+
16+
表2: `Address`
17+
```
18+
+-------------+---------+
19+
| 列名 | 类型 |
20+
+-------------+---------+
21+
| AddressId | int |
22+
| PersonId | int |
23+
| City | varchar |
24+
| State | varchar |
25+
+-------------+---------+
26+
AddressId 是上表主键
27+
```
28+
29+
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
30+
```
31+
FirstName, LastName, City, State
32+
```
33+
34+
### 解法
35+
题意中说无论 `person` 是否有地址信息,都要查出来,因此,使用左外连接查询。注意使用 `on` 关键字。
36+
37+
```sql
38+
# Write your MySQL query statement below
39+
select a.FirstName, a.LastName, b.City, b.State from Person a left join Address b on a.PersonId = b.PersonId;
40+
41+
```
42+
43+
#### Input
44+
```json
45+
{
46+
"headers": {
47+
"Person": [
48+
"PersonId",
49+
"LastName",
50+
"FirstName"
51+
],
52+
"Address": [
53+
"AddressId",
54+
"PersonId",
55+
"City",
56+
"State"
57+
]
58+
},
59+
"rows": {
60+
"Person": [
61+
[
62+
1,
63+
"Wang",
64+
"Allen"
65+
]
66+
],
67+
"Address": [
68+
[
69+
1,
70+
2,
71+
"New York City",
72+
"New York"
73+
]
74+
]
75+
}
76+
}
77+
```
78+
79+
#### Output
80+
```json
81+
{
82+
"headers": [
83+
"FirstName",
84+
"LastName",
85+
"City",
86+
"State"
87+
],
88+
"values": [
89+
[
90+
"Allen",
91+
"Wang",
92+
null,
93+
null
94+
]
95+
]
96+
}
97+
```

0 commit comments

Comments
 (0)