[align=center][size=x-large][color=red][b]内连接 外连接(左右连接)[/b][/color][/size][/align]
内连接:进行连接的两个表对应的相匹配的字段完全相同的连接。
外连接:左连接LEFT JOIN、右连接RIGHT JOIN、全连接FULL JOIN三种
左连接即LEFT OUTER JOIN:
两个表进行左连接时会返回左边表中的所有的行和右边表中与之相匹配的列值没有相匹配的用空值代替。
右连接即RIGHT OUTER JOIN:
两个表进行右连接时会返回右边表中的所有的行和左边表中与之相匹配的列值没有相匹配的用空值代替。
全连接即FULL JOIN:
满足两个表对应关系的数据集,以及不满足对应关系的其他数据
[color=red]--表stu[/color]
id name
1, Jack
2, Tom
3, Kity
4, nono
[color=red]--表exam[/color]
id grade
1, 56
2, 76
11, 89
[color=red]一、内连接[/color] (显示两表id匹配的)
select stu.id,exam.id,stu.name, exam.grade from stu inner join exam on stu.id=exam.id
stu.id exam.id name grade
--------------------------------
1 1 Jack 56
2 2 Tom 76
[color=red]二、外连接:[/color]
[color=green]左连接[/color](显示join 左边的表的所有数据,exam只有两条记录,所以stu.id,grade 都用NULL 显示)
select stu.id,exam.id,stu.name, exam.grade from stu left join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
3 NULL Kity NULL
4 NULL nono NULL
[color=green]右连接[/color](与作连接相反,显示join右边表的所有数据)
select stu.id,exam.id,stu.name, exam.grade from stu right join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
NULL 11 NULL 89
[color=green]全连接[/color]
1 1 Jack 56
2 2 Tom 76
3 NULL Kity NULL
4 NULL nono NULL
NULL 11 NULL 89
内连接:进行连接的两个表对应的相匹配的字段完全相同的连接。
外连接:左连接LEFT JOIN、右连接RIGHT JOIN、全连接FULL JOIN三种
左连接即LEFT OUTER JOIN:
两个表进行左连接时会返回左边表中的所有的行和右边表中与之相匹配的列值没有相匹配的用空值代替。
右连接即RIGHT OUTER JOIN:
两个表进行右连接时会返回右边表中的所有的行和左边表中与之相匹配的列值没有相匹配的用空值代替。
全连接即FULL JOIN:
满足两个表对应关系的数据集,以及不满足对应关系的其他数据
[color=red]--表stu[/color]
id name
1, Jack
2, Tom
3, Kity
4, nono
[color=red]--表exam[/color]
id grade
1, 56
2, 76
11, 89
[color=red]一、内连接[/color] (显示两表id匹配的)
select stu.id,exam.id,stu.name, exam.grade from stu inner join exam on stu.id=exam.id
stu.id exam.id name grade
--------------------------------
1 1 Jack 56
2 2 Tom 76
[color=red]二、外连接:[/color]
[color=green]左连接[/color](显示join 左边的表的所有数据,exam只有两条记录,所以stu.id,grade 都用NULL 显示)
select stu.id,exam.id,stu.name, exam.grade from stu left join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
3 NULL Kity NULL
4 NULL nono NULL
[color=green]右连接[/color](与作连接相反,显示join右边表的所有数据)
select stu.id,exam.id,stu.name, exam.grade from stu right join exam on stu.id=exam.id
1 1 Jack 56
2 2 Tom 76
NULL 11 NULL 89
[color=green]全连接[/color]
1 1 Jack 56
2 2 Tom 76
3 NULL Kity NULL
4 NULL nono NULL
NULL 11 NULL 89