生成测试数据
执行下面的sql语句生成简单的测试数据,包括student表和teacher表,两表通过teacher_id关联,这也是测试mysql七种连接的数据。
create table student(
id int(3) not null primary key,
name char(6) not null,
age int(2) not null,
teacher_id int(3) not null
);
create table teacher(
id int(3) not null primary key,
name char(6) not null,
age int(2) not null,
class_id int(3) not null
);
insert into student values(1, '张三', 15, 1);
insert into student values(2, '李四', 14, 2);
insert into student values(3, '王五', 16, 3);
insert into student values(4, '赵六', 13, 4);
insert into student values(5, '刘七', 14, 5);
insert into student values(6, '尼古拉斯', 15, 15);
insert into teacher values(1, 'zhang3', 15, 1);
insert into teacher values(2, 'li4', 14, 2);
insert into teacher values(3, 'wang5', 16, 3);
insert into teacher values(4, 'zhao6', 13, 4);
insert into teacher values(5, 'liu7', 14, 5);
insert into teacher values(6, 'nigula', 15, 15);
select * from student;
select * from teacher;
执行查询语句后,student表的内容如下图所示:
teacher表的查询结果如下图所示:
结合两张表的数据,可以看出student表的teacher_id 1至5和teacher表中的id 1至5,是相关联的,除此之外,student表id为6的数据没法通过teacher_Id与teacher相关联,因为id为6的数据,其teacher_id为15,而在teacher表中并没有id为15的记录,同理teacher表中id为6的数据也无法和student表相关联,是teacher表所独有。
内连接
语法:
select <select_list> from tableA A inner join tableB B on A.key = B.key;
结合测试表,执行以下sql语句:
select * from student S inner join teacher T on S.teacher_id = T.id;
查询结果如下图所示:
由查询结果可以看到student表中的teacher_id与teacher表中的id相关联,即 S.teacher_id = T.id,而内连接查询的也正是两者的交集。
左连接
语法:
select <select_list> from tableA A left join tableB B on A.key = B.key where B.key is null;
结合测试表,执行以下sql语句:
select * from student S left join teacher T on S.teacher_id = T.id where T.id is null;
查询结果如下图所示:
由查询结果可知,左连接查询的为左表独有的数据。
右链接
语法:
select <select_list> from tableA A right join tableB B on A.key = B.key where A.key is null;
结合测试表,执行以下sql语句:
select * from student S right join teacher T on S.teacher_id = T.id where S.teacher_id is null;
查询结果如下图所示:
由查询结果可知,与左连接相对应,右连接查询的为右表独有的数据。
左外连接
语法:
select <select_list> from tableA A left join tableB B on A.key = B.key;
结合测试表,执行以下sql语句:
select * from student S left join teacher T on S.teacher_id = T.id;
查询结果如下图所示:
由查询结果可知,左外连接查询的是左表独有的数据加上两表共有的数据(红圈部分)。
右外连接
语法:
select <select_list> from tableA A right join tableB B on A.key = B.key;
结合测试表,执行以下sql语句:
select * from student S right join teacher T on S.teacher_id = T.id;
查询结果如下图所示:
由查询结果可知,右外连接查询的是右表独有的数据加上两表共有的数据(红圈部分)。
全连接
语法:
select <select_list> from tableA A full outer join tableB B on A.key = B.key;(mysql不支持这种模式)
mysql不支持full outer join这种语法,但是可以通过union组合并去重实现,结合测试表,执行以下sql语句:
select * from student S left join teacher T on S.teacher_id = T.id
union
select * from student S right join teacher T on S.teacher_id = T.id;
查询结果如下图所示:
由查询结果可知,全连接查询的是左表所有的数据加上右表所有的数据并去重。
全外连接
语法:
select <select_list> from tableA A full outer join tableB B on A.key = B.key where A.key is null or B.key is null;(mysql不支持这种模式)
mysql不支持full outer join这种语法,但是可以通过union组合并去重实现,结合测试表,执行以下sql语句:
select * from student S left join teacher T on S.teacher_id = T.id where T.id is null
union
select * from student S right join teacher T on S.teacher_id = T.id where S.teacher_id is null;
查询结果如下图所示:
由查询结果可知,全连接查询的是左表独有的数据加上右表独有的数据。
结语
以上便是mysql七种连接的介绍,希望可以帮到有需要的同学!