前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >什么是内连接、外连接?MySQL支持哪些外连接?_oracle内连接和外连接的区别

什么是内连接、外连接?MySQL支持哪些外连接?_oracle内连接和外连接的区别

作者头像
全栈程序员站长
发布2022-09-27 11:24:01
9010
发布2022-09-27 11:24:01
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

图片与最后一部分来自:https://blog.csdn.net/plg17/article/details/78758593

已有如下表

rollcall 数据表

在这里插入图片描述
在这里插入图片描述

course 数据表

在这里插入图片描述
在这里插入图片描述

内链接 inner join

语句: select 表1查询的字段,表2查询的字段 from 表1 inner join 表2 on 条件; 如:

代码语言:javascript
复制
mysql> select a.*,b.* from course as a inner join rollcall as b on a.course_id=b.course_id;
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
|         1 | 1234        | 123  | 123     | 123       |       123 |  2 |         1 | 1501010096 | 于宗云       | 123          |     123 |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
1 row in set (0.07 sec)

内连接会返回两表的交集:

在这里插入图片描述
在这里插入图片描述

外连接 分为左外连接,右外连接

左外连接 left join

语句: select 表1查询的字段,表2查询的字段 from 表1 left join 表2 on 条件; // 只改变了连接的语句,其他写法相同 如:

代码语言:javascript
复制
mysql> select a.*,b.* from course as a left join rollcall as b on a.course_id=b.course_id;
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db   | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
|         1 | 1234        | 123  | 123     | 123       |       123 |    2 |         1 | 1501010096 | 于宗云       | 123          |     123 |
|         2 | 123         | 123  | 123     | 123       |       123 | NULL |      NULL | NULL       | NULL         | NULL         |    NULL |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+

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

在这里插入图片描述
在这里插入图片描述
右外连接 right join

语句: select 表1查询的字段,表2查询的字段 from 表1 right join 表2 on 条件; // 只改变了连接的语句,其他写法相同 如:

代码语言:javascript
复制
mysql> select a.*,b.* from course as a right join rollcall as b on a.course_id=b.course_id;
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
|         1 | 1234        | 123  | 123     | 123       |       123 |  2 |         1 | 1501010096 | 于宗云       | 123          |     123 |
+-----------+-------------+------+---------+-----------+-----------+----+-----------+------------+--------------+--------------+---------+
1 row in set (0.00 sec)

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

在这里插入图片描述
在这里插入图片描述

全接连

MySQL 已经没有全连接了,有的教程上还写着 full join 但是实现不了,不过可以换一种方式来查询。 语句: (SELECT * from a left JOIN b on a.name=b.id) UNION (SELECT * from a RIGHT JOIN b on a.name=b.id );

代码语言:javascript
复制
mysql> (select a.*,b.* from course as a right join rollcall as b on a.course_id=b.course_id) UNION ( select a.*,b.* from course as a LEFT join rollcall as b on a.course_id=b.course_id);
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
| course_id | course_name | time | teacher | classroom | t_command | db   | course_id | student_id | student_name | teacher_name | command |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
|         1 | 1234        | 123  | 123     | 123       |       123 |    2 |         1 | 1501010096 | 于宗云       | 123          |     123 |
|      NULL | NULL        | NULL | NULL    | NULL      |      NULL |    3 |      NULL | NULL       | NULL         | NULL         |    NULL |
|         2 | 123         | 123  | 123     | 123       |       123 | NULL |      NULL | NULL       | NULL         | NULL         |    NULL |
|         3 | 123         | 123  | 123     | 123       |       123 | NULL |      NULL | NULL       | NULL         | NULL         |    NULL |
+-----------+-------------+------+---------+-----------+-----------+------+-----------+------------+--------------+--------------+---------+
4 rows in set (0.00 sec)

补充,MySQL如何执行关联查询

**MySQL认为任何一个查询都是一次“关联”,**并不仅仅是一个查询需要到两个表匹配才叫关联,所以在MySQL中,每一个查询,每一个片段(包括子查询,甚至基于单表查询)都可以是一次关联。 当前MySQL关联执行的策略很简单:**MySQL对任何关联都执行嵌套循环关联操作,即MySQL先在一个表中循环取出单条数据,然后在嵌套循环到下一个表中寻找匹配的行,依次下去,直到找到所有表中匹配的行为止。**然后根据各个表匹配的行,返回查询中需要的各个列。请看下面的例子中的简单的查询:

查询语句:select tbl1.col1, tbl2.col2 from tbl1 inner join tbl2 using(col3) where tbl1.col1 in (5, 6); 假设MySQL按照查询中的表顺序进行关联操作,我们则可以用下面的伪代码表示MySQL将如何完成这个查询:

代码语言:javascript
复制
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
    inner_iter = iterator over tbl2 where col3 = outer_row.col3
    inner_row = inner_iter.next
    while inner_row
        output [ outer_row.col1, inner_row.col2]
        inner_row = inner_iter.next
    end
    outer_row = outer_iter.next
end

上面的执行计划对于单表查询和多表关联查询都适用,如果是一个单表查询,那么只需要上面外层的基本操作。对于外连接,上面的执行过程仍然适用。例如,我们将上面的查询语句修改如下: select tbl1.col1, tbl2.col2 from tbl1 left outer join tbl2 using(col3) where tbl1.col1 in (5, 6); 那么,对应的伪代码如下:

代码语言:javascript
复制
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
    inner_iter = iterator over tbl2 where col3 = outer_row.col3
    inner_row = inner_iter.next
    if inner_row
        while inner_row
            output [ outer_row.col1, inner_row.col2]
            inner_row = inner_iter.next
        end
    else
        output [ outer_row.col1, null]
    end
        outer_row = outer_iter.next
end

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179261.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 已有如下表
  • 内链接 inner join
  • 外连接 分为左外连接,右外连接
    • 左外连接 left join
      • 右外连接 right join
      • 全接连
      • 补充,MySQL如何执行关联查询
      相关产品与服务
      云数据库 SQL Server
      腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档