欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQL 7种链接方式

程序员文章站 2022-06-15 14:38:50
...

概要

  1. 内连接: 查询两张表*有的部分
Select <select_list>
From TableA A
Inner join TableB b
ON A.Key = B.Key
  1. 左连接:把左表的内容全查出来, 右表只查满足条件的记录
Select <select_list>
From TableA A
Left Join TableB B
ON A.Key = B.Key
  1. 右连接: 把右表的内容全部查出来, 左边只查满足条件的记录
Select <select_list>
From TableA A
Right join TableB B
On A.key = B.key
  1. 查询左边独有数据
Select <select_list>
from TableA A
left join TableB B
ON A.key = B.key
Where B.key is null
  1. 查询右表独有数据
Select <select_list>
From tableA A 
right join TableB B
ON A.key = B.key
where B.key is null
  1. 全连接: 查询两个表的全部信息(MySQL 不支持 full outer)
//(MySQL 不支持 full outer)
Select <select_list>
from TableA A
Full outer Join TableB 
ON A.Key = B.key
Select <select_list>
From TableA A
Left Join TableB B
ON A.Key = B.Key
union
Select <select_list>
From TableA A
Right join TableB B
On A.key = B.key
  1. 查询左右表各自的独有数据

//(MySQL 不支持 full outer)
Select <select_list>
From TableA 
Full Outer Join TableB B
ON A.key = B.key
Where A.key is nullor B.key is null 
Select <select_list>
from TableA A
left join TableB B
ON A.key = B.key
Where B.key is null
union
Select <select_list>
From tableA A 
right join TableB B
ON A.key = B.key
where B.key is null

SQL 7种链接方式

相关标签: 知识积累