> 学习位置:http://www.imooc.com/video/7764
**A表 user1**
| id | name |
| -- | -- |
| 1 | 唐僧 |
| 2 | 猪八戒 |
| 3 | 孙悟空 |
| 4 | 沙僧 |
**B表 user2**
| id | name |
| -- | -- |
| 1 | 孙悟空 |
| 2 | 牛魔王 |
| 3 | 蛟魔王 |
| 4 | 鹏魔王 |
| 5 | 狮驼王 |
## 内链接 INNER
> 基于连接谓词将 A、B的列组合一起,产生新的结果表
~~~
SELECT a.name,a.over,b.over
FROM user1 a
INNER JOIN user2 b
ON a.name = b.name;
~~~
![](https://box.kancloud.cn/2016-03-26_56f616539a885.png)
## 全部连接 FULL OUTER
![](https://box.kancloud.cn/2016-03-26_56f6165547f8b.png)
![](https://box.kancloud.cn/2016-03-26_56f616556cb26.png)
## 左外连接 LEFT OUTER
![](https://box.kancloud.cn/2016-03-26_56f616558c7fb.png)
~~~
SELECT a.name,a.over,b.over
FROM user1 a
LEft JOIN user2 b
ON a.name=b.name;
~~~
~~~
WHERE b.name is not null;
~~~
## 右外连接 RIGHT OUTER
![](https://box.kancloud.cn/2016-03-26_56f61655ac42b.png)
## 交叉连接 CROSS
![](https://box.kancloud.cn/2016-03-26_56f61655d1959.png)
~~~
SELECT a.name,a.over,b.name.b.over
FROM user1 a
CROSS JOIN user2 b
~~~
> 不需要on
## 优化子查询
~~~
SELECT a.name,
a.over,
(SELECT over FROM user2 b WHERE a.name = b.name) AS over2
FROM user1 a;
~~~