目录
准备:
stu 类
teacher 类
一,多表连接
1,左连接
select * from tableA left join tableB
on a.key=b.key
注:(1)左连接是以left join 为基准进行查询,left join 左边的表为左表,右边的表为右表。左连接查询的就是左表的全部信息和符合查询条件的信息(例如下图的 s.tid=t.tid,即左右两表符合关联查询条件的信息)两部分。
(2)如果左表展示出来的全部信息,右表没有相对应的信息,则右表记录为 null 。
2,右连接
select * from tableA a right join tableB b
on a.key=b.key;
注:(1)右连接是以right join 为基准进行查询,right join 左边的表为左表,右边的表为右表。右连接查询的就是右表的全部信息和符合查询条件的信息(例如下图的 s.tid=t.tid,即左右两表符合关联查询条件的信息)两部分。
(2)如果右表展示出来的全部信息,左表没有相对应的信息,则左表记录为 null 。
3,内连接
select * from tableA a inner join tableB b
on a.key=b.key;
注:inner join 内连接,展示的是两表之间的交集
4,外连接 (全连接)
select * from tableA a left join tableB b
on a.key=b.key
union //去重
select * from tableA a right join tableB b
on a.key=b.key;
5,左连接减内连接 (左独有)
select * from tableA a left join tableB b
on a.key=b.key
where b.key is null;
6,右连接减内连接 (右独有)
select * from tableA a right join tableB b
on a.key=b.key
where a.key is null;
7,外连接减内连接
select * from tableA a left join tableB b
on a.key=b.key
where b.key is null
union
select * from tableA a right join tableB b
on a.key=b.key
where a.key is null;