inner join和left join的区别

xiaoxiao2021-02-27  277

说起这两种联接方式,一定要把Right Join联系起来。

一、释义。

1、Left Join(左联接)

    以左表为中心,返回左表中符合条件的所有记录以及右表中联结字段相等的记录——当右表中无相应联接记录时,返回空值。2、Right Join(右联接)    

    以右表为中心,返回右表中符合条件的所有记录以及左表中联结字段相等的记录——当左表中无相应联接记录时,返回空值。

3、Inner Join(等值连接) 

    返回两个表中联结字段相等的行。

二、示例。

1、插入测试表(test1,test2)

create table test1  --测试表1

(id int not null,

 value char(10) )

create table test2  --测试表2

(id int not null,

 value char(10) )

2、插入数据

--insert into test1

 insert into test1

 values (1,'testaa')

 insert into test1

 values (2,'testaa')

 insert into test1

 values (3,'testaa')

--insert into test2

  insert into test2

 values (1,'testaa2')

 insert into test2

 values (2,'testaa2')

 insert into test2

 values (4,'testaa2')

3、查询结果比较(附图)

 select * from test1 a left join test2 b on a.id = b.id

 select * from test1 a right join test2 b on a.id = b.id

 select * from test1 a inner join test2 b on a.id = b.id

4、删除测试表

drop table test1 

drop table test2 

转载请注明原文地址: https://www.6miu.com/read-4308.html

最新回复(0)