当前位置:首页 > MSSQL

left join,inner join,right join,cross join,full join的区别

canca19年前 (2008-03-10)MSSQL881

 

1,总论:

LEFT JOIN返回”first_table”中所有的行尽管在” second_table”中没有相匹配的数据。

RIGHT JOIN返回”second_table”中所有的行尽管在”first_table”中没有相匹配的数据。

INNER JOIN返回的结果集是两个表中所有相匹配的数据。

2,分解:

还是用一个例子来的痛快些。。。

declare @a table(a int,b int)
declare @b table(a int,b int)

insert @a values(1,1)
insert @a values(2,2)
insert @b values(1,1)
insert @b values(3,3)

--
:
select * from @a Aa left join @b Bb on Aa.a=Bb.a
--
右:
select * from @a Aa right join @b Bb on Aa.a=Bb.a
--

select * from @a Aa join @b Bb on Aa.a=Bb.a
--
外:
select * from @a Aa full join @b Bb on Aa.a=Bb.a
--
完全
select * from @a,@b


cross join
是笛卡儿乘积 就是一张表的行数乘以另一张表的行数
left join
第一张表的连接列在第二张表中没有匹配是,第二张表中的值返回null
right join
第二张表的连接列在第一张表中没有匹配是,第一张表中的值返回null
full join
返回两张表中的行 left join+right join
inner join
只返回两张表连接列的匹配项

补充:在left join 中,如果表一中的某行在表二中有二行对应,则结果集中会有两行记录生成;同理right join。

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/475.html

标签: MSSQL
分享给朋友:

“left join,inner join,right join,cross join,full join的区别” 的相关文章

left join/right join/inner join操作演示

表A记录如下:aID        aNum1           a200501112   &nb…

两表关联统计

表1id name1  小王2  小李 表2id tid address1   1    shanhai2   1    baijin 怎样写出这样的视图呢?id 名称 地址总数1…

Can't start a cloned connection while in manual transaction mode

昨天调试程序时出现这个问题:Can't start a cloned connection while in manual transaction mode 原因一般是当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=fals…

MSSQL重置自增字段

dbcc checkident('articles',reseed,0)…

MSSQL 2000无序聚集问题

表a1 id 1 2 3 4 5 6 7 8 9 10 11 查询SQL语句: select max(id) from (select top 10 id from a1) ta 返回结果:11 没有聚集索引,表中的数据存储的序列是无序的。所以top取出的数据是不确定性的。可加order…

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。