表格如下id   籍贯   年龄
001  北京   24
002  上海   23
003  香港   22
...  ....   ...
另外一个表格如下丈夫  妻子
001   002
003   005
...   ...要求:在每行上列出每对夫妻的籍贯,年龄

解决方案 »

  1.   

    select [丈夫],[丈夫籍贯]=b.[籍贯],[丈夫年龄]=b.[年龄],[妻子],[妻子籍贯]=c.[籍贯],[妻子年龄]=c.[年龄]
    from table2 a
    join table1 b on b.[丈夫]=a.id
    join table1 c on c.[妻子]=a.id
      

  2.   

    create table tt1(id char(3),籍贯 varchar(10),年龄 int)
    create table tt2(丈夫 char(3),妻子 char(3))
    insert tt1 select '001' ,'北京','24'
    union all  select '002' ,'上海','23'
    union all  select '003' ,'香港','22'insert tt2 select '001','002'
    union  all select '003','005'select a.*,b.丈夫,c.妻子 from tt1 a
    left join tt2 b on b.丈夫=a.id
    left join tt2 c on c.妻子=a.id
      

  3.   

    create table tt1(id char(3),籍贯 varchar(10),年龄 int)
    create table tt2(丈夫 char(3),妻子 char(3))
    insert tt1 select '001' ,'北京','24'
    union all  select '002' ,'上海','23'
    union all  select '003' ,'香港','22'insert tt2 select '001','002'
    union  all select '003','005'
    select T2.丈夫,A1.籍贯,A1.年龄,T2.妻子,A2.籍贯,A2.年龄
    from 
      tt2 T2,tt1 A1,tt1 A2 
    where T2.丈夫 *=A1.id  and T2.妻子*=A2.id
      

  4.   


    select a.丈夫,b.籍贯,b.年龄,a.妻子,c.籍贯,c.年龄
    from
    tt2 a left join tt1 b on  a.丈夫=b.id
    left join tt1 c on a.妻子=c.id