区别:左连接返回包括左表中的所有记录和右表中连接字段相等的记录;右连接返回包括右表中的所有记录和左表中连接字段相等的记录;内连接只返回两个表中连接字段相等的行;全外连接返回左右表中所有的记录和左右表中连接字段相等的记录。
left join (左连接,左外连接):返回包括左表中的所有记录和右表中连接字段相等的记录。
right join (右连接,右外连接):返回包括右表中的所有记录和左表中连接字段相等的记录。
inner join (等值连接或者叫内连接):只返回两个表中连接字段相等的行。
full join (全外连接):返回左右表中所有的记录和左右表中连接字段相等的记录。
举个例子:
a表
id name
1小王
2小李
3小刘
b表
ida_idjob
12老师
24程序员
内连接:(只有2张表匹配的行才能显示)
select a.name,b.job from a a inner join b b on a.id=b.a_id
只能得到一条记录:
小李老师
左连接:(左边的表不加限制)
select a.name,b.job from a a left join b b on a.id=b.a_id
三条记录:
小王null 小李老师 小刘null
右连接:(右边的表不加限制)
select a.name,b.job from a a right join b b on a.id=b.a_id
两条记录:
小李老师 null程序员
全外连接:(左右2张表都不加限制)
select a.name,b.job from a a full join b b on a.id=b.a_id
四条数据
小王null 小李老师 小刘null null程序员
注:在sql中l外连接包括左连接(left join )和右连接(right join),全外连接(full join),等值连接(inner join)又叫内连接。
相关推荐:《sql教程》
以上就是左连接、右连接、内连接、全外连接的区别是什么?的详细内容。
