当前位置:网站首页>SQL入门——组合表
SQL入门——组合表
2022-07-26 08:53:00 【小唐学姐】
表: Person
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId 是该表的主键列。
该表包含一些人的 ID 和他们的姓和名的信息。
表: Address
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId 是该表的主键列。
该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
编写一个SQL查询来报告 Person 表中每个人的姓、名、城市和州。如果 personId 的地址不在 Address 表中,则报告为空 null 。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例 1:
输入:
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 的地址信息。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/combine-two-tables
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
数据表的连接有:
1、内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现
2、外连接: 包括
(1)左外连接(左边的表不加限制)
(2)右外连接(右边的表不加限制)
(3)满外连接(左右两表都不加限制)
3、自连接(连接发生在一张基表内)
总之:
左连接显示左边全部的和右边与左边相同的。
右连接显示右边全部的和左边与右边相同的。
内连接是只显示满足条件的部分。
# 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边栏推荐
- Store a group of positive and negative numbers respectively, and count the number of 0 -- assembly language implementation
- PHP page value transfer
- 数据库操作技能7
- day06 作业--技能题6
- 公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
- Oracle 19C OCP 1z0-083 question bank (7-12)
- CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
- Deploy prometheus+grafana monitoring platform
- Oracle 19C OCP 1z0-082 certification examination question bank (24-29)
- JDBC database connection pool (Druid Technology)
猜你喜欢

Day06 homework - skill question 6
![[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)](/img/ab/bad6b86039384af7b829bef5316923.png)
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)

第6天总结&数据库作业
![[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls](/img/8b/d5792a00fc5798d3d960f1f1b1088e.png)
[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls

Database operation skills 7

Regular expression: judge whether it conforms to USD format

Study notes of automatic control principle -- correction and synthesis of automatic control system

unity TopDown角色移动控制
![[untitled]](/img/89/c3ab79eb325f0136114a568745924b.png)
[untitled]

day06 作业--技能题2
随机推荐
Ueditot_ JSP SSRF vulnerability recurrence
Which financial product has the highest yield in 2022?
[encryption weekly] has the encryption market recovered? The cold winter still hasn't thawed out. Take stock of the major events that occurred in the encryption market last week
Espressif plays with the compilation environment
keepalived双机热备
Solve the problem of C # calling form controls across threads
node-v下载与应用、ES6模块导入与导出
Learn more about the difference between B-tree and b+tree
Day06 homework -- skill question 2
Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points
[suggestions collection] summary of MySQL 30000 word essence - locking mechanism and performance tuning (IV) [suggestions collection]
Pop up window in Win 11 opens with a new tab ---firefox
PXE principles and concepts
C Entry series (31) -- operator overloading
[untitled]
Typescript encryption tool passwordencoder
The lessons of 2000. Web3 = the third industrial revolution?
[freeswitch development practice] user defined module creation and use
day06 作业--技能题2
js闭包:函数和其词法环境的绑定