当前位置:网站首页>Getting started with SQL - combined tables
Getting started with SQL - combined tables
2022-07-26 09:55:00 【Xiao Tang Xuejie】
surface : Person
+-------------+---------+
| Name | type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId Is the primary key column of the table .
This table contains some people's ID And their last and first names .
surface : Address
+-------------+---------+
| Name | type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId Is the primary key column of the table .
Each row of the table contains a ID = PersonId Information about people's cities and states .
Write a SQL Query to report Person The last name of each person in the list 、 name 、 Cities and states . If personId Your address is not in Address In the table , The report is empty null .
With In any order Return result table .
The query result format is as follows .
Example 1:
Input :
Person surface :
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address surface :
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
Output :
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
explain :
There is no personId = 1 The address of , So their cities and States return null.
addressId = 1 Contains personId = 2 Address information for .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/combine-two-tables
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The connection of data table is :
1、 Internal connection ( Natural join ): Only rows that match two tables can appear in the result set
2、 External connection : Include
(1) The left outer join ( The table on the left is unrestricted )
(2) Right connection ( The table on the right is unrestricted )
(3) Full outer join ( There are no restrictions on the left and right tables )
3、 Self join ( The connection occurs in a base table )
All in all :
The left link shows all on the left and the same on the right and left .
The right connection shows all on the right and the same on the left and right .
The inner connection is to show only the parts that meet the conditions .
# Write your MySQL query statement below
select A.FirstName,A.LastName,B.City,B.State
FROM Person A left join Address B
on A.PersonId=B.PersonId边栏推荐
- Xiaobai makes a wave of deep copy and shallow copy
- 在.NET 6.0中配置WebHostBuilder
- Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
- M-ary number STR to n-ary number
- SSG框架Gatsby访问数据库,并显示到页面上
- QT handy notes (III) use qtcharts to draw a line chart in VS
- Keeping alive to realize MySQL automatic failover
- Node 内存溢出及V8垃圾回收机制
- R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
- 服务发现原理分析与源码解读
猜你喜欢

Fuzzy PID control of motor speed

2019 ICPC Asia Yinchuan Regional(水题题解)

Search module use case writing

SSG framework Gatsby accesses the database and displays it on the page

Registration module use case writing

Server and client dual authentication (2)

服务器内存故障预测居然可以这样做!

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

一种分布式深度学习编程新范式:Global Tensor

Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
随机推荐
在Blazor 中自定义权限验证
【信息系统项目管理师】初见高项系列精华汇总
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
Why does new public chain Aptos meet market expectations?
Fiddler download and installation
Mo team learning notes (I)
Modern medicine in the era of "Internet +"
时间序列异常检测
Node 内存溢出及V8垃圾回收机制
(1) Hand eye calibration of face scanner and manipulator (eye on hand)
高斯消元求解异或线性方程组
Server memory failure prediction can actually do this!
copyTo
Nodejs service background execution (forever)
Xiaobai makes a wave of deep copy and shallow copy
Study notes of the second week of sophomore year
新公链Aptos何以拉满市场期待值?
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
服务器内存故障预测居然可以这样做!
Fiddler packet capturing tool for mobile packet capturing