表关系类型:一对一、一对多、多对一、多对多。
多表连接 SQL 实现阶段
方式/基于主表 | 描述 |
---|---|
left/左表 | 左表所有条目都会出现,右表只出现对应上数据的条目会出现,排除右表对应不上的条目 |
right/右表 | 右表所有条目都会出现,左表只出现对应上数据的条目会出现,排除左表对应不上的条目 |
full/左右表 | 左右表所有条目均会出现 |
inner无主表 | 左右表对应数据的条目会出现,排除左右表对应不上的条目 |
-- 左连接left
select * from teacher t1 left join student t2 on t1.id = t2.stu_class;
-- 右连接 right
select * from teacher t1 right join student t2 on t1.id = t2.stu_class;
-- 全连接full
select * from teacher t1 full join student t2 on t1.id = t2.stu_class;
-- 内连接inner
select * from teacher t1 inner join student t2 on t1.id = t2.stu_class;