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


边栏推荐
- 【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
- Icml2022 | Galaxy: apprentissage actif des cartes de polarisation
- Build a highly available database
- SQL tuning guide notes 9:joins
- What is the race condition? How do you find and solve the competition?
- ICML2022 | GALAXY:極化圖主動學習
- What is your understanding of thread priority?
- 【图像去噪】基于三边滤波器实现图像去噪附matlab代码
- logstash时间戳转换为unix 纳秒nano second time
- Semester summary of freshman year
猜你喜欢

SQL调优指南笔记10:Optimizer Statistics Concepts

You can move forward or backward. This function in idea is amazing!

Jin AI her power | impact tech, she can

MySQL master-slave replication

Oracle livelabs experiment: introduction to Oracle Spatial

leetcodeSQL:574. Elected

Kotlin collaboration process - flow

SQL tuning guide notes 10:optimizer statistics concepts

Ansible playbook和Ansible Roles(三)

PCB package download website recommendation and detailed usage
随机推荐
logstash时间戳转换为unix 纳秒nano second time
[QNX hypervisor 2.2 user manual] 4.3 obtain the host component
多线程模型下的生产者消费者模式
Prefix sum and difference
【Proteus仿真】简易数码管定时器时钟
【QNX Hypervisor 2.2 用户手册】4.2 支持的构建环境
User guide for JUC concurrency Toolkit
[sword finger offer] sword finger offer 35 Replication of complex linked list
What is the difference between volatile variables and atomic variables?
Oracle livelabs experiment: introduction to Oracle Spatial
#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
Is it safe to open an account in tonghuashun? How to open an account
Ansible summary (VI)
Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases
What is the difference between a user thread and a daemon thread?
[simple] 155 Minimum stack
Icml2022 | galaxy: active learning of polarization map
孙老师版本JDBC(2022年6月12日21:34:25)
Preliminary use of jvisualvm
Ansible playbook和变量(二)