当前位置:网站首页>数据库每日一题---第10天:组合两个表
数据库每日一题---第10天:组合两个表
2022-06-12 21:56:00 【InfoQ】
一、问题描述
Person+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId 是该表的主键列。
该表包含一些人的 ID 和他们的姓和名的信息。
Address+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId 是该表的主键列。
该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
PersonpersonIdAddressnull二、题目要求
样例
输入:
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 的地址信息。
考察
1.表连接
2.建议用时10~25min
三、问题分析
SQL- A inner join B:内连接,取交集
- A left join B:左外连接,取A全部,B没有对应的值,则为
null
- A right join B:右外连接,取B全部,A没有对应的值,则为
null
- A full outer join B:全外连接,取并集,彼此没有对应的值为
null
PersonAddress迎刃而解四、编码实现
select p.firstName,p.lastName,a.city,a.state
from Person p
left join Address a on p.personId=a.personId
五、测试结果


边栏推荐
- Ansible-大总结(六)
- SQL tuning guide notes 10:optimizer statistics concepts
- Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
- ICML2022 | GALAXY:極化圖主動學習
- SQL调优指南笔记6:Explaining and Displaying Execution Plans
- #yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
- OceanBase 社区版 OCP 功能解读
- Economist focuses on WTO MC12: digital economy may become an important issue
- Digraph deep copy
- June training (day 10) - bit operation
猜你喜欢

Okio source code analysis

"Oracle database parallel execution" technical white paper reading notes
![[image denoising] image denoising based on trilateral filter with matlab code](/img/f2/770a0e2938728e731c18c0a66f7c12.png)
[image denoising] image denoising based on trilateral filter with matlab code

SQL调优指南笔记6:Explaining and Displaying Execution Plans

Dolphin-2.0.3 cluster deployment document

Oracle LiveLabs实验:Introduction to Oracle Spatial

Ansible playbook和Ansible Roles(三)

How do complex systems detect anomalies? North Carolina UNCC and others' latest overview of graph based deep learning anomaly detection methods in complex distributed systems describes the latest prog

SQL调优指南笔记14:Managing Extended Statistics

Cloning PDB with ADG standby
随机推荐
[qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
[sword finger offer] sword finger offer 58 - ii Rotate string left
Redis optimization
Oracle livelabs experiment: introduction to Oracle Spatial
SQL调优指南笔记17:Importing and Exporting Optimizer Statistics
Oracle 19C installation documentation
Data batch writing
Ansible roles project case (IV)
IPhone: save Boolean into core data - iphone: save Boolean into core data
Unity 常用3D数学计算
PCB封装下载网站推荐及其详细使用方法
OceanBase 社区版 OCP 功能解读
[Jianzhi offer] Jianzhi offer 05 Replace spaces
2021 rust survey results released: 9354 questionnaires collected
Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
MySQL介绍和安装(一)
SQL tuning guide notes 17:importing and exporting optimizer statistics
SQL tuning guide notes 16:managing historical optimizer statistics
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
MySQL architecture and basic management (II)