mysql左右连接,内全连接实例简介

inner join on:内连接:组合两个表中的记录,返回关联字段相符的记录

left join 是left outer join的简写,它的全称是左外连接,是外连接中的一种。 左(外)连接,左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。

right join是right outer join的简写,它的全称是右外连接,是外连接中的一种。与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。

union /union all:全连接,两条SQL语句查询结果组合:

1.通过union连接的SQL它们分别单独取出的列数必须相同;

2.不要求合并的表列名称相同时,以第一个sql 表列名为准;

3.使用union 时,完全相等的行,将会被合并,由于合并比较耗时,一般不直接使用 union 进行合并,而是通常采用union all 进行合并;

4.被union 连接的sql 子句,单个子句中不用写order by ,因为不会有排序的效果。但可以对最终的结果集进行排序;

注:union会自动将完全重复的数据去除掉,union all不会;

子查询:https://blog.csdn.net/saoang/article/details/123982107

实例:

student1表:
在这里插入图片描述
student2表:
在这里插入图片描述
执行:select s1.studentId,s1.name,s2.name from student1 s1 inner join student2 s2 on s1.studentId=s2.id

执行:select s1.studentId,s1.name,s2.name from student1 s1 left join student2 s2 on s1.studentId=s2.id
在这里插入图片描述
执行:select s1.studentId,s1.name,s2.name from student1 s1 right join student2 s2 on s1.studentId=s2.id

执行:(select studentId,name from student1) union (select id,name from student2)
在这里插入图片描述

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值