在SQL 中,有两个表 表 A 中的数据为  code  name 
                                  a     b1
                                  a     b2
                                  a     b3
                                  a     b4                 表 B 中的数据为  Code  name
                                  a     c1
                                  a     c2
                                  a     c3
                                  a     c4
                                  a     c5
                                  a     c6
                                  a     c7请问如果 得到 表 C  的数据为  Code  name1   name2
                            a     b1      c1
                            a     b2      c2
                            a     b3      c3
                            a     b4      c4
                            a             c5
                            a             c6
                            a             c7

解决方案 »

  1.   


       create table #temp1(code_no1 varchar(50),descript1 varchar(50))
       create table #temp2(code_no2 varchar(50),descript2 varchar(50))   insert into #temp1 values('a','b1')
       insert into #temp1 values('a','b2')
       insert into #temp1 values('a','b3')
       insert into #temp1 values('a','b4')
       insert into #temp2 values('a','c1')
       insert into #temp2 values('a','c2')
       insert into #temp2 values('a','c3')
       insert into #temp2 values('a','c4')
       insert into #temp2 values('a','c5')
       insert into #temp2 values('a','c6')
       insert into #temp2 values('a','c7')  select code_no1 as code_no,descript1,'' as descript2 from #temp1
    union
      select code_no2 as code_no,'' as descript1,descript2 from #temp2select code_no1 ,descript1,descript2 from #temp1  FULL OUTER JOIN                   #temp2 on code_no1=code_no2
     FULL OUTER JOIN 不行的  
      

  2.   

    FULL OUTER JOIN  得出来的是28条记录,不是7条记录。
      

  3.   


    create table a(code varchar(10),name varchar(10),id int identity(1,1))
    insert a
    select 'a',    'b1'  union all
    select 'a',    'b2' union all
    select 'a',    'b3' union all
    select 'a',    'b4' create table b(code varchar(10),name varchar(10),id int identity(1,1))
    insert b
    select 'a',   ' c1' union all
    select 'a',   ' c2' union all
    select 'a',   ' c3' union all
    select 'a',   ' c4' union all
    select 'a',   ' c5' union all
    select 'a',   ' c6' union all
    select 'a',   ' c7' select b.code,a.name,b.name from b left join a on a.id=b.iddrop table a
    drop table b
      

  4.   


    code       name       name       
    ---------- ---------- ---------- 
    a          b1          c1
    a          b2          c2
    a          b3          c3
    a          b4          c4
    a          NULL        c5
    a          NULL        c6
    a          NULL        c7(7 row(s) affected)
      

  5.   


    create table #temp1(code_no1 varchar(50),descript1 varchar(50)) 
      create table #temp2(code_no2 varchar(50),descript2 varchar(50))   insert into #temp1 values('a','b1') 
      insert into #temp1 values('a','b2') 
      insert into #temp1 values('a','b3') 
      insert into #temp1 values('a','b4') 
      insert into #temp2 values('a','c1') 
      insert into #temp2 values('a','c2') 
      insert into #temp2 values('a','c3') 
      insert into #temp2 values('a','c4') 
      insert into #temp2 values('a','c5') 
      insert into #temp2 values('a','c6') 
      insert into #temp2 values('a','c7') select #temp2.code_no2,#temp1.descript1,#temp2.descript2 
    from #temp1 right join  #temp2 on right(#temp1.descript1,1)=right(#temp2.descript2,1)drop table #temp1,#temp2
      

  6.   

    楼上方法很好,问题已经基本解决,把left join 换成 full join 就搞定了,判断谁连接谁!!谢谢!!
      

  7.   

    问题已经基本解决,把left join 换成 full join 就搞定了,判断谁连接谁!!谢谢!!
      

  8.   

    5楼 的方法太局限与数据,name 值不能作为关联的条件,而且temp1 表的数据不一定比temp2 表的数据多。