当前位置:网站首页>总结的太好了!终于有人把SQL的各种连接Join都讲明白了
总结的太好了!终于有人把SQL的各种连接Join都讲明白了
2022-07-17 07:05:00 【澎湖Java架构师】
SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
最常见的 JOIN 类型:SQL INNER JOIN(简单的 JOIN)、SQL LEFT JOIN、SQL RIGHT JOIN、SQL FULL JOIN,其中前一种是内连接,后三种是外链接。

假设我们有两张表,Table A是左边的表,Table B是右边的表。
一、INNER JOIN
内连接是最常见的一种连接,只连接匹配的行。
inner join语法
select column_name(s) from table 1 INNER JOIN table 2 ON table 1.column_name=table 2.column_name
注释:INNER JOIN与JOIN是相同

INNER JOIN产生的结果集中,是1和2的交集。
select * from Table A inner join Table B on Table A.id=Table B.id
执行以上SQL输出结果如下:

二、LEFT JOIN
LEFT JOIN返回左表的全部行和右表满足ON条件的行,如果左表的行在右表中没有匹配,那么这一行右表中对应数据用NULL代替。
LEFT JOIN 语法
select column_name(s) from table 1 LEFT JOIN table 2 ON table 1.column_name=table 2.column_name
注释:在某些数据库中,LEFT JOIN 称为LEFT OUTER JOIN

LEFT JOIN产生表1的完全集,而2表中匹配的则有值,没有匹配的则以null值取代。
select * from Table A left join Table B on Table A.id=Table B.id
执行以上SQL输出结果如下:

三、RIGHT JOIN
RIGHT JOIN返回右表的全部行和左表满足ON条件的行,如果右表的行在左表中没有匹配,那么这一行左表中对应数据用NULL代替。
RIGHT JOIN语法
select column_name(s) from table 1 RIGHT JOIN table 2 ON table 1.column_name=table 2.column_name
注释:在某些数据库中,RIGHT JOIN 称为RIGHT OUTER JOIN

RIGHT JOIN产生表2的完全集,而1表中匹配的则有值,没有匹配的则以null值取代。
select * from Table A right join Table B on Table A.id=Table B.id
执行以上SQL输出结果如下:

四、FULL OUTER JOIN
FULL JOIN 会从左表 和右表 那里返回所有的行。如果其中一个表的数据行在另一个表中没有匹配的行,那么对面的数据用NULL代替
FULL OUTER JOIN语法
select column_name(s) from table 1 FULL OUTER JOIN table 2 ON table 1.column_name=table 2.column_name

FULL OUTER JOIN产生1和2的并集。但是需要注意的是,对于没有匹配的记录,则会以null做为值。
select * from Table A full outer join Table B on Table A.id=Table B.id
执行以上SQL输出结果如下:

边栏推荐
- 演示集合注入
- Detailed explanation of type, user-defined type, preliminary understanding of structure
- SCA在得物DevSecOps平台上应用
- redis消息订阅
- Csp-2020-6- role authorization
- Beijing Jiewen technology, an acquiring outsourcing service provider, transferred 60% of its shares for about 480million
- Database review -- database recovery technology
- Code learning (deamnet) CVPR | adaptive consistency prior based deep network for image learning
- C语言一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
- Kingbasees can realize any of MySQL by constructing an aggregate function_ Value function.
猜你喜欢

WPF 三维应用搭建(基础)

【刷题篇】完全平方数

Shenzhen Prudential written examination record

Junit5
![[kernel] character device that drives development and learning](/img/99/2eaed37078c3245be29d82382cfd59.png)
[kernel] character device that drives development and learning

Xilinx ultrascale+ MPSoC (zu9eg/zu15eg) high performance PCIe data preprocessing board

Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card

xgboos-hperopt

2022 review questions and mock exams for main principals of hazardous chemical business units

Redis distributed lock
随机推荐
Will it be a little late to realize your "wonderful" 360?
[characteristic Engineering]
The connection between neural network and automatic control
the max_iter was reached which means the coef_ did not converge “the coef_ did not converge“
Shenzhen Prudential written examination record
C语言一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
redis数据持久化
“韭菜”是怎么把钱送给“镰刀”的? 2020-03-07
Classic general pbootcms flower website template source code, adaptive mobile terminal, with background management
Convolutional neural network CNN
Standard Version (release and changelog Automation)
依赖注入方式
如何将读取列表中的str转化为float
[kernel] character device that drives development and learning
WPF 三维应用搭建(基础)
Ku115 FPGA high performance 10G Optical fiber network hardware accelerator card / 2-way 10G Optical fiber data accelerator card
Deep learning 7 deep feedforward network
MCU如何选型?
Discussion sur la technologie RISC - V
Go语言圣经