日期:2014-05-18  浏览次数:20596 次

关于两个SQL
select   *   from   a   left   join   b   on   b.e=a.e   full   join   c   on   a.e=c.e   and   b.d=c.d
与select   *       from   a   left   join   (select   *   from   b   full   join   c   on   c.d=b.d   )   f   on   f.e=a.e
相同吗?




------解决方案--------------------
不同,
1: 取得a与b左外链接后再与c进行完全链接
2: b与c进行完全链接后再a进行左外链接的子集
显然得出的结果完全不同
------解决方案--------------------
一个是先左连接然后再用结果全连接
另一个是左连接一个全连接的结果
------解决方案--------------------
create table a(a varchar(10))
create table b(a varchar(10),b varchar(10))
create table c(a varchar(10),b varchar(10))

insert into a values ( 'a ')
insert into a values( 'b ')
insert into a values( 'c ')
insert into a values ( 'd ')


insert into b values ( 'a ', 'a ')
insert into b values ( 'b ', 'b ')
insert into b values ( 'e ', 'c ')

insert into c values ( 'a ', 'a ')
insert into c values ( 'b ', 'b ')
insert into c values ( 'd ', 'd ')
insert into c values ( 'f ', 'e ')

select * from a left
join b on b.a=a.a full join c on a.a=c.a and b.b=c.b

select * from a
left join (select isnull(b.a,c.a) as a from b full join c on c.b=b.b ) f on f.a=a.a
从例子中自己找区别吧