当前位置:网站首页>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
边栏推荐
猜你喜欢

Worth more than 100 million! The 86 version of "red boy" refuses to be a Daocheng Xueba. He is already a doctor of the Chinese Academy of Sciences and has 52 companies under his name

从ACL 2022 Onsite经历看NLP热点

1.28亿美元!芬兰量子计算公司IQM获世界基金支持

Attention should be paid to the first parameter of setTimeout

Cs224w fall course - --- 1.1 why graphs?

Compile and use protobuf Library in vs2019
Common questions and answers of software testing interview (divergent thinking, interface, performance, concept,)

周全的照护 解析LYRIQ锐歌电池安全设计

抖音服务器带宽有多大,才能供上亿人同时刷?

商城小程序项目完整源码(微信小程序)
随机推荐
HCIP第十三天笔记
Role of thread.sleep (0)
阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
Single case mode (double check lock)
批量复制宝贝上传提示乱码,如何解决?
196. 删除重复的电子邮箱
数据库红的表如何设计才能使性能更加优化
Attention should be paid to the first parameter of setTimeout
一个测试类了解BeanUtils.copyProperties
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
从ACL 2022 Onsite经历看NLP热点
win10/win11无损扩大C盘空间,跨盘合并C、E盘
ZJCTF_login
Worthington木瓜蛋白酶解离系统解决方案
八皇后编程实现
Worthington过氧化物酶活性的6种测定方法
Shell (38): SSH port forwarding
子模块cache缓存失效
【flask】服务端获取客户端请求的文件
Bulk copy baby upload prompt garbled, how to solve?