有两张表student和team表
Student
id card_id name age team_id
402881e64b5d8b54014b5d8b570f0001 s3 zhangsan 25 402881e64b5d8b54014b5d8b56f10000
402881e64b5d8b54014b5d8b570f0002 s4 zhangsan 25 402881e64b5d8b54014b5d8b56f10000
402881e64b5d8b54014b5d8b570f0003 s5 zhangsan 25 402881e64b5d8b54014b5d8b56f10000
402881e64b5d8b54014b5d8b570f0004 s6 zhangsan 25 402881e64b5d8b54014b5d8b56f10000
402881e64b5d8b54014b5d8b570f0005 s1 zhangsan 25 402881e64b5d8b54014b5d8b56f10000
402881e64b5d8b54014b5d8b570f0006 s2 zhangsan 25 402881e64b5d8b54014b5d8b56f10000
Team
id name
402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b56f10000 team2
(1) 左外连接
select*from student_3 leftjoin team ON team.id = student_3.team_id ;
id card_id name age team_id id1 name1
402881e64b5d8b54014b5d8b570f0001 s3 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0002 s4 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0003 s5 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0004 s6 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0005 s1 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0006 s2 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
以左边的表为主表。主表的所有信息都会出现在结果集中。而右表team只显示了符合条件(team.id = student_3.team_id)的记录。同理如果左表中包含了一条记录,不符合on关键字以后的条件,左表记录也会出现在结果集中,而此时右表为NULL
(2)右外连接
id card_id name age team_id id1 name1
402881e64b5d8b54014b5d8b570f0001 s3 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0002 s4 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0003 s5 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0004 s6 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0005 s1 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0006 s2 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
NULL 402881e64b5988cd014b5988cf700000 team1
右外连接以右表作为主表,主表的所有记录都会出现在结果集中。左表显示了符合on关键字的条目。如果不存在,则为null。
(3)内连接
内连接有两种
第一种不指定任何限定条件,查询结果为两个表的笛卡尔积,通常这种查询没有什么意义
select*from student_3 innerjoin team
id card_id name age team_id id1 name1
402881e64b5d8b54014b5d8b570f0001 s3 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b570f0001 s3 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0002 s4 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b570f0002 s4 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0003 s5 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b570f0003 s5 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0004 s6 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b570f0004 s6 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0005 s1 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b570f0005 s1 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0006 s2 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5988cd014b5988cf700000 team1
402881e64b5d8b54014b5d8b570f0006 s2 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
第二种带有限定条件,这种情况返回的是满足限定条件的条目。
select*from student_3 innerjoin team on team.id = student_3.team_id
id card_id name age team_id id1 name1
402881e64b5d8b54014b5d8b570f0001 s3 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0002 s4 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0003 s5 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0004 s6 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0005 s1 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
402881e64b5d8b54014b5d8b570f0006 s2 zhangsan 25 402881e64b5d8b54014b5d8b56f10000 402881e64b5d8b54014b5d8b56f10000 team2
版权声明:本文为博主原创文章,未经博主允许不得转载。