MySQL内连接、左连接、右连接的使用以及区别

时间:2019-07-07 11:07:43   收藏:0   阅读:261

首先先建两个表,student表和score表

 select * from student;

student表数据如下:

技术分享图片

 

select * from score;

score表数据如下:

技术分享图片  技术分享图片

可以看到students表中stu_id为16048008的记录对应score表没有数据;

1.内连接只显示两表中有关联的数据

select * from student inner join score on student.sid = score.stu_id;

 技术分享图片

技术分享图片

从表中可以看出student表中sid=16048008,sid=16048009,sid=160480010在score表中没有对应数据,所以内连接的结果不显示这三名学生

 

2.左连接显示左表所有数据,右表没有对应的数据用NULL补齐,多了的数据删除

select * from student left join score on student.sid = score.stu_id;

技术分享图片

技术分享图片

从结果可以看出sid=16048008,sid=16048009,sid=160480010在score表中没有数据的部分用NULL代替

 

3.右连接显示右表所有数据,左表没有对应的数据用NULL对齐,多了的数据删除

select * from student right join score on student.sid = score.stu_id;

技术分享图片

技术分享图片

score表中没有的数据student表也不显示

原文:https://www.cnblogs.com/itboxue/p/11144752.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!