有二个表:
table1:  
Field1 field2 field3
a1      b1      c1
a2      b2      c2
a3      b3      c3
a4      b4      c4table2
field1   fieldx    fieldy
a1          x1      y1
a2          x2      y2
a4          x4      y4想用一句SQL形成一个数据集:
Field1 field2 field3  fieldx   fieldy
a1      b1      c1     x1        y1
a2      b2      c2     x2        y2
a3      b3      c3
a4      b4      c4     x4        y4请问这样的SQL怎么写?谢谢!

解决方案 »

  1.   

    方法很多select field1
     , field2
     , field3
     , (select filedx from table2 t21 where t1.field1=t21.field1)
     , (select filedy from table2 t22 where t1.field1=t22.field1)
    from table1 t1
    order by t1.field1
      

  2.   

    select Table1.field1,table1.field2,table1.field3,table2.filedx,table2.filedy from table1, table2 
    where table1.field1=table2.field1
    order by table11.field1
      

  3.   

    select a.Field1,a.field2,a.field3,isnull(b.fieldx,'') as fieldx,isnull(b.fieldy,'') as fieldy
    from table1 a left join table2 b on a.field1=b.field1
      

  4.   

    select a.field1,a.field2,a.field3,b.fieldx,b.fieldy into c from a,b
      

  5.   

    select a.field1,a.field2,a.field3,isnull(b.filedx,' ') as fieldx,isnull(b.filedy,' ') as fieldy
    from table1 a, table2 b
    where a.field1=b.field1
    order by a.field1
      

  6.   

    Select a.*,IsNull(b.FieldX,'') as Fieldx,IsNull(b.Fieldy,'') as fieldy
    from table1 a
    Left join table2 b on a.field1 = b.field1
      

  7.   

    select a.Field1,a.field2,a.field3,isnull(b.fieldx,'') as fieldx,isnull(b.fieldy,'') as fieldy
    from table1 a left join table2 b on a.field1=b.field1
      

  8.   

    用Join进行连接可以实现
    select a.Field1,a.field2,a.field3,b.fieldx,b.fieldy
    from table1 a left join table2 b on a.field1=b.field1 
    --and  …… -- 若有需要
      

  9.   

    ORACLE:
    select a.Field1,a.field2,a.field3,b.fieldx,b.fieldy
    from table1 a, table2 b
    where a.field1(+)=b.field1
      

  10.   

    SELECT TABLE1.* ,TABLE2.* FROM TABLE1,TABLE2
    WHERE TABLE1.field1 = table2.field1