left join和right join(左连接、右链接)

外连接分两种:left join和right join

left join主表是左表,right join主表是右表

外连接查询结果会显示主表所有的数据,如果从表没有匹配的数据就默认显示为null

语法格式:select(字段) from (表1) left join (表2)on (关联的字段)

left join举例:

grade表                                                                               student表

      

 如图,主表是grade,从表是student表

根据grade表中的id和student表中的id,进行关联

student表中没有id为1006对应的数据,所以默认显示null

right join举例:

 如图,主表是student,从表是grade表

根据grade表中的id和student表中的id,进行关联

grade表中没有id为1005对应的数据,所以默认显示null

posted @ 2023-04-25 10:59  呆萌呆萌呆萌  阅读(191)  评论(0编辑  收藏  举报