---
title: "左连接、右连接、外连接、内连接、全连接"
author: "xiangsheng"
date: "2011-03-16T17:32:13Z"
tags: []
categories: ["数据库"]
slug: "untitled"
draft: false
lastmod: "2011-03-16T17:32:13Z"
id: 964503
url: "https://xiangsheng.iteye.com/blog/964503"
---

表：
test1
id  name
1	aaa
2	bbb
3	ccc

test2
id  name
1	eee
1	fff
3	ggg


左连接也称左外连接(右连接同理)：将左表的所有记录列出，右表中只要符合on条件的，与左表记录相拼合，不符合条件的，填以null值。
例：select t1.*,t2.* from test1 t1 left join test2 t2 on t1.id=t2.id
结果：
id name id name
1	aaa	1	eee
1	aaa	1	fff
3	ccc	3	ggg
2	bbb	null null

内连接：根据左连接来说，不会将左表的所有记录列出，不符合条件的，不填null值，只找符合条件的。
例：select t1.*,t2.* from test1 t1 inner join test2 t2 on t1.id=t2.id
结果：
id name id name
1	aaa	1	eee
1	aaa	1	fff
3	ccc	3	ggg

全连接：左连接+右连接，把重复的合并。
例：select t1.*,t2.* from test1 t1 full join test2 t2 on t1.id=t2.id
结果：
id name id name
1	aaa	1	eee
1	aaa	1	fff
3	ccc	3	ggg
2	bbb	null null

多表联查：笛卡尔积
例：select t1.*,t2.* from test1 t1,test2 t2
结果：
id name id name
1	aaa	1	eee
1	aaa	1	fff
1	aaa	3	ggg
2	bbb	1	eee
2	bbb	1	fff
2	bbb	3	ggg
3	ccc	1	eee
3	ccc	1	fff
3	ccc	3	ggg