当前位置:网站首页>数据库每日一题---第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
五、测试结果


边栏推荐
- SQL tuning guide notes 11:histograms
- Use group_ Dplyr issues when using group_ by(multiple variables)
- OceanBase 社区版 OCP 功能解读
- Vagrantbox reinstalling the vboxsf driver
- One article to quickly understand whether there are security risks in your network
- Exception encountered by selenium operation element
- Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
- 最近公共祖先问题你真的学会了吗?
- IPhone: save Boolean into core data - iphone: save Boolean into core data
- Ansible playbook and variable (II)
猜你喜欢

如何自己动手写一个vscode插件,实现插件自由!

Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)

Ansible playbook and variable (II)

SQL调优指南笔记16:Managing Historical Optimizer Statistics

A puzzle about + =

SQL调优指南笔记9:Joins

SQL tuning guide notes 13:gathering optimizer statistics
![[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

Ansible playbook和Ansible Roles(三)

回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
随机推荐
Cloning PDB with ADG standby
One article to quickly understand whether there are security risks in your network
Turing prize winner: what should I pay attention to if I want to succeed in my academic career?
logstash时间戳转换为unix 纳秒nano second time
[qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
MySQL architecture and basic management (II)
ICML2022 | GALAXY:極化圖主動學習
Leetcode Yanghui triangle
Icml2022 | Galaxy: apprentissage actif des cartes de polarisation
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
Have you really learned the common ancestor problem recently?
Build a highly available database
PCB封装下载网站推荐及其详细使用方法
关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
What is the difference between volatile variables and atomic variables?
如何自己动手写一个vscode插件,实现插件自由!
talent showing itself! Oceanbase was selected into the 2021 "sci tech innovation China" open source innovation list
NiO User Guide
Vagrantbox reinstalling the vboxsf driver
Interpretation of OCP function of oceanbase Community Edition