[LeetCode] 175. Combine Two Tables

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



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.


解題邏輯與實作

原來LeetCode還有出資料庫的題目阿,不過題目看來不多,而且題目也不太難,所以手癢稍微試了一下。

Left Join

這題看起來就是聯合查表的題目,檢查一下兩張Table的欄位,發現PersonId這欄位兩張表都有且命名也相同,所以我選它直接使用Using來進行聯合。

# Write your MySQL query statement below
SELECT p.FirstName, p.LastName, a.City, a.State 
FROM Person p LEFT OUTER JOIN Address a USING (PersonId);



[LeetCode] 解題目錄 -> 這裡走

留言

這個網誌中的熱門文章

用Markdown寫Blogger文章

【Vue.js 學習筆記】02. 基礎 Vue 概述