mysql 交叉连接_MySQL中的内连接、左连接、右连接、全连接、交叉连接

本文详细介绍了MySQL中的各种连接查询,包括内连接、左连接、右连接、全连接以及交叉连接。通过实例展示了不同连接查询的SQL语句及执行结果,帮助读者理解各种连接查询的差异和应用场景。
摘要由CSDN通过智能技术生成

创建两个表(a_table、b_table),两个表的关联字段分别为:a_table.a_id和b_table.b_id

CREATE TABLEa_table (

a_idint NOT NULL,

a_namevarchar(10) DEFAULT NULL,

a_partvarchar(10) DEFAULT NULL);CREATE TABLEb_table (

b_idint(11) DEFAULT NULL,

b_namevarchar(10) DEFAULT NULL,

b_partvarchar(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8

分别向两个表中插入数据:

a_table:                b_table:

dfe79f4207e9c9b8808502819dbac120.png                  

5376801c0e2f13fdf6d980506f8613cd.png

一、内连接

说明:组合两个表中的记录,返回关联字段相符的记录,即:两个表的交集(阴影)部分。

5f1f23cd8b7088fe7a45411d3e8a5c80.png

关键字:inner join on

SQL语句:

select * from a_table a inner join b_table b on a.a_id = b.b_id;

执行结果:

d73557ba20c36e6f45741dd9ccbd1fb6.png

二、左连接(左外连接)

说明:左连接全称是左外连接,是外连接中的一种。

左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录,右表记录不符合搜索条件的地方均为NULL。

3c32a0989bdd3d94ccf82bea5470ba6e.png

关键字:left join on / left outer join on(left join 是left outer join的简写)

SQL语句:

select * from a_table a left join b_table b on a.a_id =b.b_id;select * from a_table a left outer join b_table b on a.a_id = b.b_id;

执行结果:

f5da3f94828e421eda3d31b55bf5ed89.png

---------------------------------------------------

当两个表的记录行数不同时,以左表为准。

例:

a_table有四条记录:            b_table有五条记录:

c8216be0ed3b51409009e65744943d73.png                       

ec56f9f2d08125c22c9ed713b7aa1a16.png

SQL语句:

select * from a_table as a left join b_table as b on a.a_id = b.b_id;

执行结果:

ecea3c8fe5f9e93358a16509c0e3e064.png

三、右连接(右外连接)

说明:右连接的全称是右外连接,是外连接中的一种。

与左连接相反,右连接中,右表(b_table)的记录将会全部表示出来,左表(a_table)只会显示符合搜索条件的记录,记录不符合搜索条件的地方均为NULL。

3e29cff61253652bf69745c3b3f954de.png

关键字:right join on / right outer join on(right join是right outer join的简写)

SQL语句:

select * from a_table a right joinb_table b on a.a_id =b.b_id;select * from a_table a right outer join b_table bon a.a_id = b.b_id;

执行结果:

d6df99bea833d2b7b4748b825912d52a.png

---------------------------------------------------

当两个表的记录行数不同时,以右表为准。

例:

a_table有四条记录:            b_table有五条记录:

c8216be0ed3b51409009e65744943d73.png                       

ec56f9f2d08125c22c9ed713b7aa1a16.png

SQL语句:

select * from a_table as a right join b_table as b on a.a_id = b.b_id;

执行结果:

66bfc4a51ec34f57535fa7c468005f44.png

四、全连接(全外连接)

说明:返回左右表的所有行。哪个表中没有的就用null填充。

关键字:MySQL不支持全外连接,所以只能采取关键字UNION来联合左、右连接

SQL语句:

select * from a_table as a left join b_table as b on a.a_id =b.b_idUNION

select * from a_table as a right join b_table as b ona.a_id = b.b_id;

执行结果:

eea0aa4bd7eaa1c4584afd86a21f3982.png

五、交叉连接

说明:没有where条件的交叉连接中,产生连接表就是笛卡尔积。将两个表的所有行进行组合,连接后的行数为两个表的行数的乘积数。

关键字:cross join

1. SQL语句:

select * from a_tablecross joinb_table;

#等价于:select * from a_table, b_table;

执行结果:

fde50c7928636d13f2d1fe128bc159d0.png

2. SQL语句

select * from a_table as a cross join b_table as b on a.a_id = b.b_id;

执行结果:

a3ee18f26a246c12a88a4a29e36d4ec8.png

六、自连接

说明:自连接是连接的一种用法,但并不是连接的一种类型,因为它的本质是把一张表当成两张表来使用。

例子:https://www.cnblogs.com/liangwh520/p/8299481.html

七、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将如何完成这个查询:

outer_iter = iterator over tbl1 where col1 in (5, 6)

outer_row= outer_iter.next

whileouter_row

inner_iter= iterator over tbl2 where col3 =outer_row.col3

inner_row= inner_iter.next

whileinner_row

output[outer_row.col1, inner_row.col2]inner_row= inner_iter.next

endouter_row= outer_iter.next

end

上面的执行计划对于单表查询和多表关联查询都适用,如果是一个单表查询,那么只需要上面外层的基本操作。

对于外连接,上面的执行过程仍然适用。例如,我们将上面的查询语句修改如下:

select tbl1.col1, tbl2.col2 from tbl1 left outer join tbl2 using(col3) where tbl1.col1 in (5, 6);

那么,对应的伪代码如下:

outer_iter = iterator over tbl1 where col1 in (5, 6)

outer_row= outer_iter.next

whileouter_row

inner_iter= iterator over tbl2 where col3 =outer_row.col3

inner_row= inner_iter.next

ifinner_rowwhileinner_row

output[outer_row.col1, inner_row.col2]inner_row= inner_iter.next

end

elseoutput[outer_row.col1, null]

endouter_row= outer_iter.next

end

note:SQL语句中的using:

在做等值连接的时候,若A表中和B表中的列名相同,则可以使用using简写,例:

a_table                  b_table

042500fa566593e6689f749f49bf8640.png    

2245c7975d70b39c859a871fd8842ff3.png

1. 使用等值连接

select * from a_table a right join b_table b on a.id = b.id;

0adfc9fc90f1f0ff951ca5fa62fddf4d.png

2. 使用using

select * from a_table a right join b_table b using(id);

39aa4625db54e264b54cac92f889ef3d.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值