左连接、右连接、内连接、全外连接的区别是什么?
时间:2022-02-25 11:50
区别:左连接返回包括左表中的所有记录和右表中连接字段相等的记录;右连接返回包括右表中的所有记录和左表中连接字段相等的记录;内连接只返回两个表中连接字段相等的行;全外连接返回左右表中所有的记录和左右表中连接字段相等的记录。 left join (左连接,左外连接):返回包括左表中的所有记录和右表中连接字段相等的记录。 right join (右连接,右外连接):返回包括右表中的所有记录和左表中连接字段相等的记录。 inner join (等值连接或者叫内连接):只返回两个表中连接字段相等的行。 full join (全外连接):返回左右表中所有的记录和左右表中连接字段相等的记录。 举个例子: A表 id name 1 小王 2 小李 3 小刘 B表 id A_id job 1 2 老师 2 4 程序员 内连接:(只有2张表匹配的行才能显示) 只能得到一条记录: 左连接:(左边的表不加限制) 三条记录: 右连接:(右边的表不加限制) 两条记录: 全外连接:(左右2张表都不加限制) 四条数据 注:在sql中l外连接包括左连接(left join )和右连接(right join),全外连接(full join),等值连接(inner join)又叫内连接。 相关推荐:《SQL教程》 以上就是左连接、右连接、内连接、全外连接的区别是什么?的详细内容,更多请关注gxlsystem.com其它相关文章!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 程序员
select a.name,b.job from A a full join B b on a.id=b.A_id
小王 null
小李 老师
小刘 null
null 程序员