日期:2014-05-17  浏览次数:20787 次

full join 在不同数据库有不同结果,知道的一定要近来看下(内详)
两张表   :
表aa  
字段:  
  ID   INT   自动增长
  客户   char(10)
  物品   char(10)
  数量   int
表bb  
字段:  
  ID   INT   自动增长
  客户   char(10)
  物品   char(10)
  数量   int

表aa   :
数据:
  ID   客户   物品   数量
  1     1           1         1


表bb   :
数据:
  ID   客户   物品   数量
  1     2           2         3
  2     2           2         1
  3     1           1         4

查询语句:
select   a1.*,b1.*
from
(select   客户,物品,sum(数量)   from   aa   group   by   客户,物品)a1
full   join
(select   客户,物品,sum(数量)   from   bb   group   by   客户,物品)b1
on   a1.客户=b1.客户   and   a1.数量=b1.数量

在本地数据:结果是
  a1.客户   物品   数量   b1.客户   物品   数量
        1         1         1             1             1         4
        NULL   NULL   NULL       2             2         4
在另一数据:结果是
  a1.客户   物品   数量   b1.客户   物品   数量
        1         1         1             1             1         4
        NULL   NULL   NULL       2             2         null
        NULL   NULL   NULL       2             2         null

b1里面的(没有跟A对应起来的)数据的分组效果就没有了。郁闷,望有人能知道原因!


------解决方案--------------------
没有遇到过这样的情况


a1.客户 物品 数量 b1.客户 物品 数量
1 1 1 1 1 4
NULL NULL NULL 2 2 null
NULL NULL NULL 2 2 null


数量为什么是null?
------解决方案--------------------
create table test2 (c1 int,c2 varchar2(10),c3 varchar2(10),c4 int);
create table test3 (c1 int,c2 varchar2(10),c3 varchar2(10),c4 int);

insert into test2 values(1, '1 ', '1 ',1);

insert into test3 values(1, '2 ', '2 ',3);
insert into test3 values(2, '2 ', '2 ',1);
insert into test3 values(3, '1 ', '1 ',4);


select a1.*,b1.*
from
(select c2,c3,sum(c4) c4 from test2 group by c2,c3) a1
full join
(select c2,c3,sum(c4) c4 from test3 group by c2,c3) b1
on a1.c2=b1.c2 and a1.c3=b1.c3


C2 C3 C4 C2 C3 C4
---------- ---------- ---------- ---------- ---------- ----------
1 1 1 1 1 4
2 2
2 2