当前位置:网站首页>数据库每日一题---第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 的人的城市和州的信息。
Person
personId
Address
null
二、题目要求
样例
输入:
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
Person
Address
迎刃而解
四、编码实现
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 用户手册】4.4 构建Host
- Use group_ Dplyr issues when using group_ by(multiple variables)
- Cloning PDB with ADG standby
- NiO User Guide
- Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
- Oracle livelabs experiment: introduction to Oracle Spatial
- Recommended Chinese font in the code input box of Oracle SQL developer
- What are thread scheduler and timeslicing?
- SQL调优指南笔记10:Optimizer Statistics Concepts
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
猜你喜欢
Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
SQL调优指南笔记13:Gathering Optimizer Statistics
多线程模型下的生产者消费者模式
SQL调优指南笔记14:Managing Extended Statistics
User guide for JUC concurrency Toolkit
NiO User Guide
Ansible summary (VI)
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
Icml2022 | galaxy: active learning of polarization map
最近公共祖先问题你真的学会了吗?
随机推荐
脱颖而出!OceanBase 入选 2021“科创中国”开源创新榜单
大学期间零基础如何开展编程学习
OceanBase 社区版 OCP 功能解读
My struggle: my years in foreign enterprises (1)
#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
【图像去噪】基于三边滤波器实现图像去噪附matlab代码
[medium] 78 Subset (backtracking shall be supplemented later)
SQL tuning guide notes 9:joins
[sword finger offer] sword finger offer 35 Replication of complex linked list
SQL tuning guide notes 16:managing historical optimizer statistics
lambda表达式与流优化代码
Gzip compression decompression
SQL tuning guide notes 14:managing extended statistics
jsonUtils
PCB封装下载网站推荐及其详细使用方法
Producer consumer model under multithreading model
JVisualVM初步使用
Zip compression decompression
IPhone: save Boolean into core data - iphone: save Boolean into core data
[sword finger offer simple] sword finger offer 24 Reverse linked list