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


边栏推荐
- [qnx hypervisor 2.2 manuel de l'utilisateur] 4.2 environnement de construction pris en charge
- What are thread scheduler and timeslicing?
- Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
- Interpretation of OCP function of oceanbase Community Edition
- Ansible-大总结(六)
- Semester summary of freshman year
- The kotlin coroutine -- coroutine context and exception propagation
- 经济学人聚焦WTO MC12:数字经济或成重要议题
- 【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
- 在同花顺开户证券安全吗,证券开户怎么开户流程
猜你喜欢

"Oracle database parallel execution" technical white paper reading notes

MySQL architecture and basic management (II)

IPhone: save Boolean into core data - iphone: save Boolean into core data

SQL调优指南笔记9:Joins

Ansible playbook and variable (II)

SQL tuning guide notes 13:gathering optimizer statistics

Ansible-大总结(六)

A puzzle about + =

How to write a vscode plug-in by yourself to realize plug-in freedom!

Okio source code analysis
随机推荐
【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
How to prevent phishing emails? S/mime certificate to help!
Interpretation of OCP function of oceanbase Community Edition
Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
MySQL introduction and installation (I)
What is the race condition? How do you find and solve the competition?
多线程模型下的生产者消费者模式
Ansible foundation and common modules (I)
A puzzle about + =
leetcodeSQL:574. Elected
Unity commonly used 3D mathematical calculation
SQL调优指南笔记9:Joins
SQL tuning guide notes 8:optimizer access paths
SQL tuning guide notes 12:configuring options for optimizer statistics gathering
#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
建立高可用的数据库
Economist focuses on WTO MC12: digital economy may become an important issue
jsonUtils
SQL调优指南笔记13:Gathering Optimizer Statistics