1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/组合两个表 [combine-two-tables].html

70 lines
2.2 KiB
HTML
Raw Normal View History

2022-03-27 20:56:26 +08:00
<p>表: <code>Person</code></p>
<pre>
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
2023-12-09 18:42:21 +08:00
personId 是该表的主键(具有唯一值的列)。
该表包含一些人的 ID 和他们的姓和名的信息。
2022-03-27 20:56:26 +08:00
</pre>
<p>&nbsp;</p>
<p>表: <code>Address</code></p>
<pre>
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
2023-12-09 18:42:21 +08:00
addressId 是该表的主键(具有唯一值的列)。
该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
2022-03-27 20:56:26 +08:00
</pre>
<p>&nbsp;</p>
2023-12-09 18:42:21 +08:00
<p>编写解决方案,报告 <code>Person</code> 表中每个人的姓、名、城市和州。如果 <code>personId</code> 的地址不在&nbsp;<code>Address</code>&nbsp;表中,则报告为&nbsp;<code>null</code>&nbsp;</p>
2022-03-27 20:56:26 +08:00
<p><strong>任意顺序</strong> 返回结果表。</p>
2023-12-09 18:42:21 +08:00
<p>结果格式如下所示。</p>
2022-03-27 20:56:26 +08:00
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>
Person表:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address表:
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
<strong>输出:</strong>
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
<strong>解释:</strong>
2023-12-09 18:42:21 +08:00
地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。
2022-03-27 20:56:26 +08:00
addressId = 1 包含了 personId = 2 的地址信息。</pre>