除非两张表中均有表示行号的字段如row:
则可用select * from tablea a left join tableb b on a.row=b.row

解决方案 »

  1.   


    --如果两表没有关系,则要用临时表或其他方法为它们创造关系,否则无法处理
    select id=identity(int),* into #t1 from tableA
    select id=identity(int),* into #t2 from tableB
    select a=isnull(a.a,''),b=isnull(a.b,''),c=isnull(a.c,''),d=isnull(a.d,'')
    ,a1=isnull(b.a1,''),b1=isnull(b.b1,''),c1=isnull(b.c1,''),d1=isnull(b.d1,'')
    from #t1 a full join #t2 b on a.id=b.id
    drop table #t1,#t2
      

  2.   

    你沒有關聯﹐譯會產生邊卡樂積運算select * from (select a,b,c,d from tableA)a 
    cross join (select a1,b1,c1,d1 from tableB)b
      

  3.   

    两表之间有关系: 
    tableA.d=tableB.d1
    要得到结果是,如果tableB没有的内容,则显示为空.
      

  4.   

    tableA.d=tableB.d1 ,这种关系无法得到楼主的结果,因为这是一种多对多的关系.
      

  5.   

    建议楼主先仔细考虑一下自己要做什么,建立相应的关系
    比如:
    1、如果希望按照存入记录的顺序对应tableA和tableB且还在表设计阶段,最好分别在两表增加id列(属性设为种子,实际插入记录时该列会自动增加),那么你的查询语句就可以非常简单的写成select * from tableA a left join tableB b on a.id = b.id
    当然,如果你是这样需要的但是两个表已经投入使用不能更改相关字段了,只好用zjcxc(邹建) 老师四楼的办法
    2、如果你的两个表中记录都有某种关系通过第三者对应,假设第三者为表xx中的字段,则可以写成:
    select
      

  6.   

    正在尝试中……
     hdhai9451(※★開拓者...糊涂中☆※)  的方式得不到所要的结果,并且速度很慢.
    谢谢 adaizi1980(阿代) 的建议,因为我的tableA和tableB也是临时表,所以加上ID列应该没什么问题。
    尝试中……
    谢谢各位……
      

  7.   

    不好意思,刚才点错了,没写完就发出去了,这里继续select a,b,c,d,a1,b1,c1,d1 from (select a,b,c,d,c.对应字段 as 对应字段 from tableA,c where tableA和c的关系) a left join (select a,b,c,d,c.对应字段 as 对应字段 from tableB,c where tableB和c的关系) B on a.对应字段 = b.对应字段 3、如果两个表没有任何关系,也不是1中所说的情况,我也不知道你想做什么
      

  8.   

    我试了。不行,原因如下:
    因为tableA和tableB的自动增长ID并不是完全相等,有时候tableA到17,
    tableB却只有16,它并不会自己增加上空值.
    结果成了这样:
     a    b    c      d    a1    b1    c1      d1
    025    01   red    16   051    02   red    16
    001    03   red    16   075    04   red    16
    038    05   red    16   064    06   red    16
    036    07   red    16   069    08   red    16   
    011    09   red    16   025    01   red    17
    然后就全部乱了。
      

  9.   

    to :adaizi1980(阿代)
    的第2种方法有点看不懂,可以帮忙解释一下吗?
      

  10.   

    select a.*,b.* from tableA a 
    left join tableB b on a.d=b.d1
      

  11.   

    zjcxc(邹建) 已经说的很清楚了
    tableA.d=tableB.d1 ,这种关系无法得到楼主的结果,因为这是一种多对多的关系.
      

  12.   

    创造临时表来解决  转zjcxc(邹建):
    select id=identity(int),* into #t1 from tableA
    select id=identity(int),* into #t2 from tableB
    select a=isnull(a.a,''),b=isnull(a.b,''),c=isnull(a.c,''),d=isnull(a.d,'')
    ,a1=isnull(b.a1,''),b1=isnull(b.b1,''),c1=isnull(b.c1,''),d1=isnull(b.d1,'')
    from #t1 a full join #t2 b on a.id=b.id
    drop table #t1,#t2
      

  13.   

    谢谢楼上的各位大侠.
    创造临时表方法是不错,可是我在上面已举例说明了。结果成了如下:
     a    b    c      d    a1    b1    c1      d1
    025    01   red    16   051    02   red    16
    001    03   red    16   075    04   red    16
    038    05   red    16   064    06   red    16
    036    07   red    16   069    08   red    16   
    011    09   red    16   025    01   red    17 --请看此最后一条数据
    然后就全部乱了。
    本来最后一条数据为空的。应该这样:
     a    b    c      d    a1    b1    c1      d1
    025    01   red    16   051    02   red    16
    001    03   red    16   075    04   red    16
    038    05   red    16   064    06   red    16
    036    07   red    16   069    08   red    16   
    011    09   red    16
    但如上面所给的方法,得不到此结果,因为ID自动增长的数据不能一一对应。
      

  14.   

    --示例--测试数据
    create table ta(a varchar(6),b varchar(6),c varchar(6),d varchar(6))
    insert ta select '025','01','red',16
    union all select '001','03','red',16
    union all select '038','05','red',16
    union all select '036','07','red',16
    union all select '011','09','red',16
    union all select '011','09','red',18
    union all select '011','09','red',18create table tb(a1 varchar(6),b1 varchar(6),c1 varchar(6),d1 varchar(6))
    insert tb select '051','02','red',16
    union all select '075','04','red',16
    union all select '064','06','red',16
    union all select '069','08','red',16
    union all select '025','01','red',17
    union all select '025','01','red',18
    go--查询处理
    select id=identity(int),* into #t1 from ta
    select id=identity(int),* into #t2 from tbselect a=isnull(a.a,''),b=isnull(a.b,''),c=isnull(a.c,''),d=isnull(a.d,'')
    ,a1=isnull(b.a1,''),b1=isnull(b.b1,''),c1=isnull(b.c1,''),d1=isnull(b.d1,'')
    from #t1 a full join #t2 b 
    on a.d=b.d1
    and (select count(*) from #t1 where id<=a.id and d=a.d)
    =(select count(*) from #t2 where id<=b.id and d1=b.d1)
    order by isnull(a.d,b.d1)
    drop table #t1,#t2
    go--删除测试
    drop table ta,tb/*--测试结果a      b      c      d      a1     b1     c1     d1     
    ------ ------ ------ ------ ------ ------ ------ ------ 
    025    01     red    16     051    02     red    16
    001    03     red    16     075    04     red    16
    038    05     red    16     064    06     red    16
    036    07     red    16     069    08     red    16
    011    09     red    16                          
                                025    01     red    17
    011    09     red    18     025    01     red    18
    011    09     red    18                          (所影响的行数为 8 行)
    --*/
      

  15.   

    谢谢zjcxc(邹建) 大侠.
    结果差不多就是这样.
    还有点小问题,结果要求的是这样的。
    a      b      c      d      a1     b1     c1     d1     
    ------ ------ ------ ------ ------ ------ ------ ------ 
    025    01     red    16     051    02     red    16
    001    03     red    16     075    04     red    16
    038    05     red    16     064    06     red    16
    036    07     red    16     069    08     red    16
    011    09     red    16                          
    025    01     red    17
    025    01     red    18                            
    011    09     red    18     
    011    09     red    18 
    也就是说 b 为奇数的永远在左边,b1
      

  16.   

    --测试数据
    create table ta(a varchar(6),b varchar(6),c varchar(6),d varchar(6))
    insert ta select '025','01','red',16
    union all select '001','03','red',16
    union all select '038','05','red',16
    union all select '036','07','red',16
    union all select '011','09','red',16
    union all select '011','09','red',18
    union all select '011','09','red',18create table tb(a1 varchar(6),b1 varchar(6),c1 varchar(6),d1 varchar(6))
    insert tb select '051','02','red',16
    union all select '075','04','red',16
    union all select '064','06','red',16
    union all select '069','08','red',16
    union all select '025','01','red',17
    union all select '025','01','red',18
    go--查询
    select id=identity(int),* into #t1 
    from(
    select a,b,c,d from ta
    union all
    select a1,b1,c1,d1 from tb where b1%2=1
    )a
    select id=identity(int),* into #t2 from tb where b1%2=0select a=isnull(a.a,''),b=isnull(a.b,''),c=isnull(a.c,''),d=isnull(a.d,'')
    ,a1=isnull(b.a1,''),b1=isnull(b.b1,''),c1=isnull(b.c1,''),d1=isnull(b.d1,'')
    from #t1 a full join #t2 b 
    on a.d=b.d1
    and (select count(*) from #t1 where id<=a.id and d=a.d)
    =(select count(*) from #t2 where id<=b.id and d1=b.d1)
    order by isnull(a.d,b.d1)
    drop table #t1,#t2
    go--删除测试
    drop table ta,tb/*--测试结果a      b      c      d      a1     b1     c1     d1     
    ------ ------ ------ ------ ------ ------ ------ ------ 
    025    01     red    16     051    02     red    16
    001    03     red    16     075    04     red    16
    038    05     red    16     064    06     red    16
    036    07     red    16     069    08     red    16
    011    09     red    16                          
    025    01     red    17                          
    025    01     red    18                          
    011    09     red    18                          
    011    09     red    18                          (所影响的行数为 9 行)
    --*/
      

  17.   

    两个表都有为奇数的怎么处理? 
    ---
    这个情况已排除
    既然已经排除,那你还考虑什么? 我的测试是因为tableB表中有b列为奇数的,既然你能保证为b列为奇数的始终在tableA表,那处理方法不需要做任何改动
      

  18.   

    谢谢 zjcxc(邹建) 大侠……
    问题已解决……
      

  19.   

    可以自已找关系啊!
    select *
    from tablea a left join tableb b on a.b=b.b1-1
    不知能不?