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
你贴的sql似乎有点问题条件应该是这样的吧on a1.c2=b1.c2 and a1.c3=b1.c3
[解决办法]
create table t11 (c2 varchar2(10),c3 varchar2(10),c4 int)
create table t12 (c2 varchar2(10),c3 varchar2(10),c4 int)
insert into t11 values( '1 ', '1 ',1)
insert into t12 values( '1 ', '1 ',4)
insert into t12 values( '2 ', '2 ',4)
select a1.*,b1.*
from
t11 a1
full join
t12 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 4
怪了???
[解决办法]
http://www.itpub.net/300771.html
9201的bug?
[解决办法]
你的SQL是不是应该:
select a1.*,b1.*
from
(select 客户,物品,sum(数量) as aa from aa group by 客户,物品)a1
full join
(select 客户,物品,sum(数量) as bb from bb group by 客户,物品)b1
on a1.客户=b1.客户 and a1.aa=b1.bb
---------------------------------------
另确认你打了Oracle的最新补丁(9.0.2.6),9.0.1是有Full Join的问题。
[解决办法]
感觉很奇怪,我是根据你的情况做的测试,9i中我没有用过full join
