|
@@ -1,7 +1,5 @@
|
|
|
# 多表操作
|
|
|
|
|
|
-关于 SQL 语句中的连接 (Join) 关键字,是较为常用而又不太容易理解的关键字,下面这个例子给出了一个简单的解释。
|
|
|
-
|
|
|
```sql
|
|
|
--建表 table1,table2:
|
|
|
create table table1(id int, name varchar(10))
|
|
@@ -16,15 +14,36 @@ insert into table2 select 3, 70
|
|
|
```
|
|
|
|
|
|
如表
|
|
|
-
|
|
|
-```
|
|
|
- table1 | table2
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- id name | id score
|
|
|
- 1 lee | 1 90
|
|
|
- 2 zhang | 2 100
|
|
|
- 4 wang | 3 70
|
|
|
-```
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th colspan="2">table1</th>
|
|
|
+ <th colspan="2">table2</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
|
|
|
以下均在查询分析器中执行
|
|
|
|
|
@@ -32,7 +51,9 @@ insert into table2 select 3, 70
|
|
|
|
|
|
### 1、概念
|
|
|
|
|
|
-内联接是用比较运算符比较要联接列的值的联接(join 或 inner join)
|
|
|
+内联接:也称等值连接,返回两张表都满足条件的部分
|
|
|
+
|
|
|
+> 注释:inner join 就等于 join
|
|
|
|
|
|
### 2、SQL 语句
|
|
|
|
|
@@ -42,14 +63,27 @@ select * from table1 inner join table2 on table1.id = table2.id
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- id name id score
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- 1 lee 1 90
|
|
|
- 2 zhang 2 100
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 只返回符合条件的 table1 和 table2 的列
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:只返回符合条件的 table1 和 table2 的列
|
|
|
|
|
|
与下列执行效果相同
|
|
|
|
|
@@ -58,17 +92,17 @@ A: select a.*, b.* from table1 a, table2 b where a.id = b.id
|
|
|
B: select * from table1 cross join table2 where table1.id = table2.id
|
|
|
```
|
|
|
|
|
|
-[^注]: cross join 后加条件只能用 where,不能用 on
|
|
|
+> 注释:cross join 后加条件只能用 where,不能用 on
|
|
|
|
|
|
## 二、外连接
|
|
|
|
|
|
### 1、概念
|
|
|
|
|
|
-包括左向外联接、右向外联接或完整外部联接
|
|
|
+包括左向外联接(left join)、右向外联接(right join)或完整外部联接
|
|
|
|
|
|
### 2、左连接:left join 或 left outer join
|
|
|
|
|
|
-(1)左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。
|
|
|
+(1)左向外联接的结果集包括 left outer 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。
|
|
|
|
|
|
(2)SQL 语句
|
|
|
|
|
@@ -78,15 +112,33 @@ select * from table1 left join table2 on table1.id = table2.id
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- id name id score
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- 1 lee 1 90
|
|
|
- 2 zhang 2 100
|
|
|
- 4 wang NULL NULL
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 包含 table1 的所有子句,根据指定条件返回 table2 相应的字段,不符合的以 null 显示
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>NULL</td>
|
|
|
+ <td>NULL</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:包含 table1 的所有子句,根据指定条件返回 table2 相应的字段,不符合的以 null 显示
|
|
|
|
|
|
### 3、右连接:right join 或 right outer join
|
|
|
|
|
@@ -100,15 +152,33 @@ select * from table1 right join table2 on table1.id = table2.id
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- id name id score
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- 1 lee 1 90
|
|
|
- 2 zhang 2 100
|
|
|
- NULL NULL 3 70
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 包含 table2 的所有子句,根据指定条件返回 table1 相应的字段,不符合的以 null 显示
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>NULL</td>
|
|
|
+ <td>NULL</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:包含 table2 的所有子句,根据指定条件返回 table1 相应的字段,不符合的以 null 显示
|
|
|
|
|
|
### 4、完整外部联接:full join 或 full outer join
|
|
|
|
|
@@ -122,16 +192,39 @@ select * from table1 full join table2 on table1.id = table2.id
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- id name id score
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- 1 lee 1 90
|
|
|
- 2 zhang 2 100
|
|
|
- 4 wang NULL NULL
|
|
|
- NULL NULL 3 70
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 返回左右连接的和(见上左、右连接)
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>NULL</td>
|
|
|
+ <td>NULL</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>NULL</td>
|
|
|
+ <td>NULL</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:返回左右连接的和(见上左、右连接)
|
|
|
|
|
|
## 三、交叉连接(完全)
|
|
|
|
|
@@ -147,21 +240,69 @@ select * from table1 cross join table2
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- id name id score
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- 1 lee 1 90
|
|
|
- 2 zhang 1 90
|
|
|
- 4 wang 1 90
|
|
|
- 1 lee 2 100
|
|
|
- 2 zhang 2 100
|
|
|
- 4 wang 2 100
|
|
|
- 1 lee 3 70
|
|
|
- 2 zhang 3 70
|
|
|
- 4 wang 3 70
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 返回 3*3=9 条记录,即笛卡尔积
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:返回 3*3=9 条记录,即笛卡尔积
|
|
|
|
|
|
等价(与下列执行效果相同)
|
|
|
|
|
@@ -183,14 +324,27 @@ insert into table3 select 3, null
|
|
|
|
|
|
如表
|
|
|
|
|
|
-```
|
|
|
- table3
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- id score
|
|
|
- 1 null
|
|
|
- 2 null
|
|
|
- 4 null
|
|
|
-```
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th colspan="2">table3</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>NULL</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>NULL</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>NULL</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
|
|
|
将 table2 中的 score 更新到 table3 中
|
|
|
|
|
@@ -200,14 +354,27 @@ update b set b.score = a.score from table2 a, table3 b where a.id = b.id
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- table3
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- id score
|
|
|
- 1 90
|
|
|
- 2 100
|
|
|
- 4 70
|
|
|
-```
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th colspan="2">table3</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
|
|
|
### 2、多表删除
|
|
|
|
|
@@ -219,27 +386,70 @@ delete from table1 where not exists(select 1 from table2 b where table1.id = b.i
|
|
|
|
|
|
删除前
|
|
|
|
|
|
-```
|
|
|
- table1 | table2
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- id name | id score
|
|
|
- 1 lee | 1 90
|
|
|
- 2 zhang | 2 100
|
|
|
- 4 wang | 3 70
|
|
|
-```
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th colspan="2">table1</th>
|
|
|
+ <th colspan="2">table2</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
|
|
|
删除后
|
|
|
|
|
|
-```
|
|
|
- table1 | table2
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- id name | id score
|
|
|
- 1 lee | 1 90
|
|
|
- 2 zhang | 2 100
|
|
|
- | 3 70
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 将 table2 表中未使用的 table1 表编号删除
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th colspan="2">table1</th>
|
|
|
+ <th colspan="2">table2</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>1</td>
|
|
|
+ <td>lee</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>2</td>
|
|
|
+ <td>zhang</td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td></td>
|
|
|
+ <td></td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:将 table2 表中未使用的 table1 表编号删除
|
|
|
|
|
|
|
|
|
|
|
@@ -251,14 +461,35 @@ delete from table1 where exists(select 1 from table2 b where table1.id = b.id)
|
|
|
|
|
|
结果
|
|
|
|
|
|
-```
|
|
|
- table1 | table2
|
|
|
------------------------------------------------------------------------------------------
|
|
|
- id name | id score
|
|
|
- 4 wang | 1 90
|
|
|
- | 2 100
|
|
|
- | 3 70
|
|
|
-```
|
|
|
-
|
|
|
-[^注释]: 将 table2 表中使用的 table1 表编号删除
|
|
|
+<table>
|
|
|
+<tr align="center">
|
|
|
+ <th colspan="2">table1</th>
|
|
|
+ <th colspan="2">table2</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <th>id</th>
|
|
|
+ <th>name</th>
|
|
|
+ <th>id</th>
|
|
|
+ <th>score</th>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td>4</td>
|
|
|
+ <td>wang</td>
|
|
|
+ <td>1</td>
|
|
|
+ <td>90</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td></td>
|
|
|
+ <td></td>
|
|
|
+ <td>2</td>
|
|
|
+ <td>100</td>
|
|
|
+</tr>
|
|
|
+<tr align="center">
|
|
|
+ <td></td>
|
|
|
+ <td></td>
|
|
|
+ <td>3</td>
|
|
|
+ <td>70</td>
|
|
|
+</tr>
|
|
|
+</table>
|
|
|
+> 注释:将 table2 表中使用的 table1 表编号删除
|
|
|
|