博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 175. Combine Two Tables
阅读量:7097 次
发布时间:2019-06-28

本文共 1873 字,大约阅读时间需要 6 分钟。

https://leetcode.com/problems/combine-two-tables/description/

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

 
  • Since the PersonId in table Address is the foreign key of table Person, we can join this two table to get the address information of a person. 

  • Considering there might not be an address information for every person, we should use outer join instead of the default inner join.

  • Note: Using where clause to filter the records will fail if there is no address information for a person because it will not display the name information.

 

1 Create table If Not Exists Person (PersonId int, FirstName varchar(255), LastName varchar(255)); 2 Create table If Not Exists Address (AddressId int, PersonId int, City varchar(255), State varchar(255)); 3 Truncate table Person; 4 insert into Person (PersonId, LastName, FirstName) values ('1', 'Wang', 'Allen'); 5 Truncate table Address; 6 insert into Address (AddressId, PersonId, City, State) values ('1', '2', 'New York City', 'New York'); 7  8 # Write your MySQL query statement below 9 select P.FirstName, P.LastName, A.City, A.State 10 from Person P left join Address A 11 on P.PersonId = A.PersonId;
View Code

 

 

 

 

转载于:https://www.cnblogs.com/pegasus923/p/7652884.html

你可能感兴趣的文章
CHIL-SQL-约束 (Constraints)
查看>>
64位操作系统在DOSBox中进入debug的问题
查看>>
ArrayList源码分析
查看>>
WiFi无线连接过程中有哪几个主要步骤?
查看>>
C++之编码问题(Unicode,ASCII,本地默认)
查看>>
[日常] DNS的迭代查询过程
查看>>
[Linux] Nginx 提供静态内容和优化积压队列
查看>>
Excel VBA 基本概念
查看>>
获取文件Md5值
查看>>
Linux常用命令整理
查看>>
逛论坛时发现 有关 递归调用
查看>>
JavaScript的3大组成部分&&ECMAScript函数&&闭包
查看>>
.NET Core 跨平台发布(dotnet publish)
查看>>
OpenAcc社区版安装教程(Linux版)(更新版)
查看>>
java写入文件的几种方法
查看>>
NGINX模块(一)
查看>>
Linux service命令
查看>>
TCP发送源码学习(2)--tcp_write_xmit
查看>>
Android第三方开源图片裁剪截取:cropper
查看>>
C# 中英文符号互转
查看>>