[LeetCode] Combine Two Tables 組合兩個表

中文題目

表1: Person

+-------------+---------+
| 列名 | 類型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId 是上表主鍵.
表2: Address

+-------------+---------+
| 列名 | 類型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId 是上表主鍵.

寫一個 SQL 查詢語句,滿足條件:無論 person 是否有地址信息,都需要基于上述兩表提供 person 的以下信息:

FirstName, LastName, City, State


英文題目

Table: Person

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

FirstName, LastName, City, State


參考答案

方法一:

SELECT p.FirstName, p.LastName, a.City, a.State FROM Person2 p LEFT JOIN Address a USING (PersonId)

方法二:

SELECT p.FirstName, p.LastName, a.City, a.State FROM Person2 p LEFT JOIN Address a ON(p.PersonId = a.PersonId)

方法三:

SELECT p.FirstName, p.LastName, a.City, a.State FROM Person2 p,Address a WHERE p.PersonId = a.PersonId
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容