当前位置:网站首页>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
边栏推荐
- ES6模块化导入导出)(实现页面嵌套)
- Typescript snowflake primary key generator
- idea快捷键 alt实现整列操作
- One click deployment of lamp and LNMP scripts is worth having
- Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field
- Which of count (*), count (primary key ID), count (field) and count (1) in MySQL is more efficient? "Suggested collection"
- The idea shortcut key ALT realizes the whole column operation
- Implementation of Prometheus web authentication and alarm
- Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points
- Foundry tutorial: writing scalable smart contracts in various ways (Part 1)
猜你喜欢
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
Pop up window in Win 11 opens with a new tab ---firefox
QtCreator报错:You need to set an executable in the custom run configuration.
(1) CTS tradefed test framework environment construction
Form form
pl/sql之集合-2
Probability model in machine learning
Regular expression: judge whether it conforms to USD format
day06 作业--技能题2
Review notes of Microcomputer Principles -- zoufengxing
随机推荐
(1) CTS tradefed test framework environment construction
mysql函数
[untitled]
Database operation topic 2
QtCreator报错:You need to set an executable in the custom run configuration.
[untitled]
Replication of SQL injection vulnerability in the foreground of Pan micro e-cology8
TypeScript版加密工具PasswordEncoder
day06 作业--技能题6
Database operation skills 7
The largest number of statistical absolute values --- assembly language
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
SSH,NFS,FTP
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
数据库操作 题目二
机器学习中的概率模型
The idea shortcut key ALT realizes the whole column operation
Database operation topic 1
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
数据库操作技能7