当前位置:网站首页>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
边栏推荐
- 解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
- Fuzzy PID control of motor speed
- The use of MySQL in nodejs
- Leetcode 504. Hex number
- Rowselection emptying in a-table
- POJ 1012 Joseph
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
- 反射机制的原理是什么?
- The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
- protobuf的基本用法
猜你喜欢
服务器内存故障预测居然可以这样做!
protobuf的基本用法
Interview shock 68: why does TCP need three handshakes?
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
服务发现原理分析与源码解读
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
小白搞一波深拷贝 浅拷贝
Applet record
挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
随机推荐
JS table auto cycle scrolling, mouse move in pause
分布式网络通信框架:本地服务怎么发布成RPC服务
面试突击68:为什么 TCP 需要 3 次握手?
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
JS 一行代码 获取数组最大值与最小值
Use of selectors
IE7 set overflow attribute failure solution
莫队学习笔记(一)
Gauss elimination solves the inverse of matrix (Gauss)
服务器内存故障预测居然可以这样做!
Interview shock 68: why does TCP need three handshakes?
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
js 表格自动循环滚动,鼠标移入暂停
Development to testing: a six-year road to automation starting from 0
Sqoop [put it into practice 02] sqoop latest version full database import + data filtering + field type support description and example code (query parameter and field type forced conversion)
Interview shock 68: why does TCP need three handshakes?
Learning notes: what are the common array APIs that change the original array or do not change the original array?
The whole process of server environment configuration
面试突击68:为什么 TCP 需要 3 次握手?
Interpretation of the standard of software programming level examination for teenagers_ second level