各种SQL连接符Join

一、连接符分类,内连接,外连接

1、内连接:Inner Join简写Join。

2、外连接:Left Outer Join 简写Left Join;Right Outer Join 简写Right Join;Full Outer Join 简写Full Join。

二、用集合的形式展示各个连接符的特点

1、准备测试表Test_StudentA,Test_StudentB

2、Join,返回Test_StudentA与Test_StudentB集合的交集。

3、Left Join, 返回左表Test_StudentA的全集,右表匹配连接条件有值,不匹配赋NULL值。

4、Right Join,返回右表Test_StudentB的全集,左表匹配连接条件有值,不匹配赋NULL值。

5、Full Join,返回左表Test_StudentA,右表Test_StudentB的全集,不匹配连接条件赋NULL值。

三、数据库中展现Join,Left Join,Right Join,Full Join的不同

1、Join,返回左表与右表符合ON连接条件的行。

如:select * from Test_StudentA join Test_StudentB on Test_StudentA.id=Test_StudentB.id

2、Left Join, 以左表为基表,返回左表所有行。若右表不符合ON连接条件,则对应的字段赋NULL值。

如:select * from Test_StudentA left join Test_StudentB on Test_StudentA.id=Test_StudentB.id

3、Right Join,以右表为基表,返回右表所有行。若左表不符合ON连接条件,则对应的字段赋NULL值。

如:select * from Test_StudentA right join Test_StudentB on Test_StudentA.id=Test_StudentB.id

4、Full Join,返回左右表所有行,不符合on条件的字段赋NULL值。

如:select * from Test_StudentA full join Test_StudentB on Test_StudentA.id=Test_StudentB.id

猜你喜欢

转载自www.cnblogs.com/handhead/p/11097248.html