首先先建两个表,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表也不显示

posted on 2019-07-06 23:47  深海收破烂  阅读(1792)  评论(0编辑  收藏  举报