当前位置:网站首页>175. Combine two tables (very simple)
175. Combine two tables (very simple)
2022-07-27 03:13:00 【only-qi】
Problem description :
surface : Person
+-------------+---------+
| Name | type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId Is the primary key column of the table .
This table contains some people's ID And their last and first names .
surface : Address
+-------------+---------+
| Name | type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId Is the primary key column of the table .
Each row of the table contains a ID = PersonId Information about people's cities and states .
Write a SQL Query to report Person The last name of each person in the list 、 name 、 Cities and states . If personId Your address is not in Address In the table , The report is empty null .
With In any order Return result table .
The query result format is as follows .
Example :
Example 1:
Input :
Person surface :
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address surface :
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
Output :
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
explain :
There is no personId = 1 The address of , So their cities and States return null.
addressId = 1 Contains personId = 2 Address information for .
Pass times 403,646
Submit the number 547,216
On sql, Take it to run :
CREATE TABLE `address` (
`AddressId` int(10) NOT NULL AUTO_INCREMENT,
`PersonId` int(10) DEFAULT NULL,
`City` varchar(255) DEFAULT NULL,
`State` varchar(255) DEFAULT NULL,
PRIMARY KEY (`AddressId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
CREATE TABLE `person` (
`PersonId` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(255) DEFAULT NULL,
`LastName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`PersonId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;SELECT p.FirstName,p.LastName,a.City,a.State from person as p left join address as a on a.PersonId=p.PersonIdI want to brush it 300 Algorithm questions , The first 108 Avenue
边栏推荐
猜你喜欢

Worthington过氧化物酶活性的6种测定方法

Hcip day 14 notes

身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
The most complete basic knowledge of software testing in the whole network (a must for beginners)

Cs224w fall course - --- 1.1 why graphs?
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)

Ten thousand words long text, take you to understand the kubernetes network model

OpenTelemetry 在服务网格架构下的最佳实践
![Analysis of [paper] pointlanenet papers](/img/f6/8001be4f90fe15100e0295de02491f.png)
Analysis of [paper] pointlanenet papers

185. All employees with the top three highest wages in the Department (mandatory)
随机推荐
[hash table] question collection
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
How big is the bandwidth of the Tiktok server for hundreds of millions of people to brush at the same time?
周全的照护 解析LYRIQ锐歌电池安全设计
Redis四大特殊数据类型的学习和理解
[哈希表] 刷题合集
Worthington果胶酶的特性及测定方案
Baidu cloud face recognition
数模1232
Hcip day 14 notes
Go to export excel form
单例模式(双检锁)
智能指针shared_ptr、unique_ptr、weak_ptr
177. 第N高的薪水(简单)
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
OpenTelemetry 在服务网格架构下的最佳实践
身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
Social wechat applet of fanzhihu forum community
coco test-dev 测试代码