现在的位置: 首页 > 综合 > 正文

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

2012年05月07日 ⁄ 综合 ⁄ 共 660字 ⁄ 字号 评论关闭

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
只返回两张表连接列的匹配项

抱歉!评论已关闭.