表A 表B 表C 都有字段col,查询方式一:
表A full join 表B on A.col = B.col full join 表C on A.col = C.col 查询方式二:
表A full join 表B on A.col = B.col full join 表C on B.col = C.col这两种查询方式查询出的结果为什么不一样????????
如果换成left join 或者 inner join 呢? 在线等!!!!!!!!!
表A full join 表B on A.col = B.col full join 表C on A.col = C.col 查询方式二:
表A full join 表B on A.col = B.col full join 表C on B.col = C.col这两种查询方式查询出的结果为什么不一样????????
如果换成left join 或者 inner join 呢? 在线等!!!!!!!!!
if object_id('Tempdb..#a') is not null drop table #a
if object_id('Tempdb..#b') is not null drop table #b
if object_id('Tempdb..#c') is not null drop table #ccreate table #a(aid int identity(1,1) not null,cola int null)
create table #b(bid int identity(1,1) not null,colb int null)
create table #c(cid int identity(1,1) not null,colc int null)Insert Into #a
select 1 union all
select 2 union all
select 3 Insert Into #b
select 2 union all
select 3 union all
select 4Insert Into #c
select 3 union all
select 4 union all
select 5
-------查询
select * from #a full join #b on aid=bid full join #c on aid=cid
select * from #a full join #b on aid=bid full join #c on bid=cid
-------结果aid cola bid colb cid colc
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 2 1 3
2 2 2 3 2 4
3 3 3 4 3 5(3 行受影响)aid cola bid colb cid colc
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 2 1 3
2 2 2 3 2 4
3 3 3 4 3 5
(SELECT * FROM 表A full join 表B on A.col = B.col) A1
full join 表C on A1.col = C.col
(SELECT isnull(A.col,B.col) col,其它字段 FROM 表A full join 表B on A.col = B.col) A1
full join 表C on A1.col = C.col
查询方式一,如果B和C都有值1,如果A没有1,他们是不会在一条记录里的
查询方式二,数据同上,就会在一条记录里
TBL_B 有字段 COL(3,4), COL_B(B1,B2)
TBL_C 有字段 COL(2,3), COL_C(C1,C2)表A full join 表B on A.col = B.col 就是从表A中取字段COL中值(1,2)与B表中的COL字段一一进行比较,如果有相等的,有几行,则返回几行,如果没有相等的则该字段为空,
再从表B中取出字段COL中的值(3,4)与表A中的COL一一比较,如果有相当的则不再检索出来,如果没有相等的则该字段为空,。得出的结果集合为
也就是从上个集合中,找出属于A表的COL字段(有4行,2行为空,1行为1,1行为2)与表C中发COL字段(2,3)进行一一比较,再取表C中的COL字段与上个集合中属于表A的COL进行一一的比较。
得出结果如下: