日期:2014-05-19  浏览次数:20443 次

我要分别搜索两张不同表的3个子段,遇到大问题。SQL的问题
第一张表3个字段:A1,B1,C1
第二张表3个字段:A2,B2,C2

搜索这6个字段出来
A1,B1,C1,A2,B2,C2
放进Dataset当中

我需要的结果是
当   任意2张表的字段值不同就空行

数据例子是这样的:

A1     B1     C1     A2     B2     C2
1       1       1       1       1       1
1       1       2       1       1       2
1       1       3      
1       2       1       1       2       1
                        1       2       2
                        1       2       3
2       1       1       2       1       1
2       1       2      

搜索出来有6列,前3列来自一张表,后三列来自一张表
前三列的在后三列中没有,则   空行
后三列在前三列中没有,则空行

这就是数据例子   不知道大家看懂了没有
请问SQL文我该怎么写


不好意思     小弟分不多了

------解决方案--------------------
select * from
表1 a full join 表2 b
on b.A2=a.A1 and b.B2=a.B1 and b.C2=a.C1
order by isnull(A1,A2), isnull(B1,B2), isnull(C1,C2)
------解决方案--------------------
不用谢~~什么问题~~说出我谢你

create table 表1(A1 int, B1 int, C1 int)
insert into 表1 select 1, 1, 1
union all select 1, 1, 2
union all select 1, 1, 3
union all select 1, 2, 1
union all select 2, 1, 1
union all select 2, 1, 2


create table 表2(A2 int, B2 int, C2 int)
insert into 表2 select 1, 1, 1
union all select 1, 1, 2
union all select 1, 2, 1
union all select 1, 2, 2
union all select 1, 2, 3
union all select 2, 1, 1


select * from
表1 a full join 表2 b
on b.A2=a.A1 and b.B2=a.B1 and b.C2=a.C1
order by isnull(A1,A2), isnull(B1,B2), isnull(C1,C2)


A1 B1 C1 A2 B2 C2
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 1 1 1
1 1 2 1 1 2
1 1 3 NULL NULL NULL
1 2 1 1 2 1
NULL NULL NULL 1 2 2
NULL NULL NULL 1 2 3
2 1 1 2 1 1
2 1 2 NULL NULL NULL