关于SQL中内连接,左外连接,右外连接,全连接的一些见解

当我们在学习sql的时候,表连接是必不可少的学习只是,下面,我就总结下sql表连接里用到的四种连接:内连接,左外连接,右外连接,全连接
1.内连接,inner join…on,使用例句select * from table1 t1 inner join table2 t2 on t1.id=t2.id 表示两张表中只显示相交的部分,即符合on后面语句的部分
新建表table1并插入数据如图
新建表table1如图
新建表table2并插入数据如图:
在这里插入图片描述
执行select * from table1 t1 inner join table2 t2 on t1.id=t2.id;语句后得到结果如图:
在这里插入图片描述
2.左连接:left join…on,使用例句select * from table1 t1 left join table2 t2 on t1.id=t2.id,结果会显示左边表即第一张表所有的数据,第二张表符合on后要求的显示出来,不符合要求的用null显示
执行select * from table1 t1 left join table2 t2 on t1.id=t2.id;语句后得到的效果如图:
在这里插入图片描述
3.右连接:right join…on,使用例句select * from table1 t1 right join table2 t2 on t1.id=t2.id,结果会显示右边表即第二张表所有的数据,第二张表符合on后要求的显示出来,不符合要求的用null显示
执行select * from table1 t1 right join table2 t2 on t1.id=t2.id;语句后得到的效果如图:在这里插入图片描述
4.全连接:full join…on,使用例句select * from table1 t1 full join table2 t2 on t1.id=t2.id,结果会显示两张表所有的项,不符合要求的用null显示
执行select * from table1 t1 full join table2 t2 on t1.id=t2.id;语句后得到的效果如图:
在这里插入图片描述
注意:mysql不支持full join …on,可以用union来实现语句如下:
select * from table1 t1 left join table2 t2 on t1.id=t2.id
union
select * from table1 t1 right join table2 t2 on t1.id=t2.id

发布了12 篇原创文章 · 获赞 1 · 访问量 143

猜你喜欢

转载自blog.csdn.net/u010574271/article/details/103419703