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
2022-03-29 12:43:11 +08:00

70 lines
2.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>表: <code>Person</code></p>
<pre>
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId是该表的主键列。
该表包含一些人的ID和他们的姓和名的信息。
</pre>
<p>&nbsp;</p>
<p>表: <code>Address</code></p>
<pre>
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId是该表的主键列。
该表的每一行都包含一个ID = PersonId的人的城市和州的信息。
</pre>
<p>&nbsp;</p>
<p>编写一个SQL查询来报告 <code>Person</code> 表中每个人的姓、名、城市和状态。如果 <code>personId</code> 的地址不在&nbsp;<code>Address</code>&nbsp;表中,则报告为空 &nbsp;<code>null</code>&nbsp;</p>
<p><strong>任意顺序</strong> 返回结果表。</p>
<p>查询结果格式如下所示。</p>
<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>
地址表中没有 personId = 1 的地址所以它们的城市和州返回null。
addressId = 1 包含了 personId = 2 的地址信息。</pre>