连续参加的两场面试都问到了,例子是转载的。
1.内连接(inner join)
只将两个表中满足条件的数据展示出来
2.左连接(left join)
例如 a left join b
将左表a的数据完全显示,b表仅展示匹配(满足条件)的数据,没有匹配的显示为空NULL。
3.右连接(right join)
例如a right join b
将右表b的数据完全显示,a表仅展示匹配(满足条件)的数据,没有匹配的显示为空NULL。
4完全连接(full join)
返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的列显示空值。
例子:
a表 id name b表 id job parent_id
1 张3 1 23 1
2 李四 2 34 2
3 王武 3 34 4
a.id同parent_id 存在关系
1.内连接:select a.,b. from a inner join b on a.id=b.parent_id
2.左连接:select a.,b. from a left join b on a.id=b.parent_id
3.右连接:select a.,b. from a right join b on a.id=b.parent_id
4.完全连接:select a.,b. from a full join b on a.id=b.parent_id
本来结果应该是
1 张3 1 23 1
2 李四 2 34 2
null 3 34 4
3 王武 null
(这个不知道为啥出错了,知道的同学可以给我留言)
例子来源:https://blog.csdn.net/qq_38192784/article/details/74469066