当前位置:网站首页>175. 组合两个表(非常简单)
175. 组合两个表(非常简单)
2022-07-27 00:43:00 【only-qi】
问题描述:
表: Person
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId 是该表的主键列。
该表包含一些人的 ID 和他们的姓和名的信息。
表: Address
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId 是该表的主键列。
该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
编写一个SQL查询来报告 Person 表中每个人的姓、名、城市和州。如果 personId 的地址不在 Address 表中,则报告为空 null 。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例 :
示例 1:
输入:
Person表:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address表:
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
输出:
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
解释:
地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。
addressId = 1 包含了 personId = 2 的地址信息。
通过次数403,646
提交次数547,216
上sql,拿去即可运行:
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.PersonId我要刷300道算法题,第108道
边栏推荐
- 论构造函数的原型是谁
- Concept of data asset management
- 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
- Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
- 快速排序(Quick sort)
- [dynamic programming simple question] leetcode 53. maximum subarray and
- Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
- Ansible series: do not collect host information gather_ facts: False
- CuteOne:一款OneDrive多网盘挂载程序/带会员/同步等功能
- How to use devaxpress WPF to create the first MVVM application in winui?
猜你喜欢

The EXE compiled by QT is started with administrator privileges

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

阿里云解决方案架构师张平:云原生数字化安全生产的体系建设

CS224W fall 1.2 Applications of Graph ML

Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换

What is a process?

Use the most primitive method to manually implement the common 20 array methods

毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了

static关键字

Time module: acquisition and mutual transformation of timestamp, structured time and formatted time
随机推荐
五、MFC视图窗口和文档
Shell (38): SSH port forwarding
How to do the system security test? Let's talk about it in detail
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
[动态规划简单题] LeetCode 53. 最大子数组和
[redis] quick start
Is the low commission account opening of Galaxy Securities Fund reliable, reliable and safe
云开发寝适闹钟微信小程序源码
杀毒软件 clamav 的安装和使用
[哈希表] 刷题合集
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
CS224W fall 1.2 Applications of Graph ML
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
Scheduling of processes
Debezium series: pull historical data based on debezium offset to ensure that data is not lost
一道数学题,让芯片巨头亏了5亿美金!
[binary search medium] leetcode 34. find the first and last positions of elements in the sorted array
Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"
Leetcode- > binary search clock in