两张表做匹配
table1               table2
col1   col2            col1       col2
1       aa              1        12-df.fas-aa.hh
2       bb              2        fdsa-cc.mk-123
3       cc              3        32-bb.iuyi-87987asdf
结果为
table3
col1    col2    col3
1        aa     12-df.fas-aa.hh
2        bb     32-bb.iuyi-87987asdf
3        cc     fdsa-cc.mk-123意思就是在table2中的col2中找到包含table1中col2中的记录插入table1的第三列 
谢谢各位大哥,在线等!

解决方案 »

  1.   

    SELECT A.COL1,A.COL2,COL3 = B.COL2 INTO TABLE3 FROM TABLE1 A LEFT JOIN TABLE2 B ON A.COL1 = B.COL1
      

  2.   

    Select A.*, B.col2 As col3
    From table1 A Inner Join table2 B
    On A.col1 = B.col1
      

  3.   

    Select A.*, B.col2 As col3
    From table1 A, table2 B Where A.col1 = B.col1
      

  4.   

    如果是要生成一個新表Select A.*, B.col2 As col3 Into table3
    From table1 A Inner Join table2 B
    On A.col1 = B.col1或者Select A.*, B.col2 As col3 Into table3
    From table1 A, table2 B Where A.col1 = B.col1
      

  5.   

    SELECT table1.col1,table1.col2,table2.col2 AS col3
     FROM table1  INNER JOIN table2 ON table1.col1=table2.col1
      

  6.   

    --刚才没看清楚你的意思SELECT A.COL1,A.COL2,COL3 = B.COL2 INTO TABLE3 FROM TABLE1 A LEFT JOIN TABLE2 B ON  CHARINDEX(A.COL2,B.COL2) > 0
      

  7.   

    大哥们,这里的col1是没用的,不能用他们做匹配啊
      

  8.   

    意思就是在table2中的col2中找到包含table1中col2中的记录插入table1的第三列 
    ...........
    你们看帖哇~晕死
      

  9.   

    暈,的確沒看清
    Select A.*, B.col2 As col3
    From table1 A Inner Join table2 B
    On B.col2 Like '%' + A.col2 + '%'Select A.*, B.col2 As col3
    From table1 A, table2 B Where B.col2 Like '%' + A.col2 + '%'
      

  10.   

    create table #table1(col1 int, col2 varchar(20) )
    insert into #table1 
    select 1 ,      'aa'  union all
    select 2 ,      'bb'  union all
    select 3 ,      'cc' 
    create table #table2(col1 int, col2 varchar(20) )
    insert into #table2 
    select 1 ,      '12-df.fas-aa.hh'  union all
    select 2 ,      'fdsa-cc.mk-123'  union all
    select 3 ,      '32-bb.iuyi-87987asdf'select a.*,b.col2
    from #table1 a , #table2 b
    where CHARINDEX (a.col2,b.col2)>0
      

  11.   

    如果table1与table2会一对多,加多个MAX(b.col2)
      

  12.   

    --结果
    col1        col2                 col2                 
    ----------- -------------------- -------------------- 
    1           aa                   12-df.fas-aa.hh
    3           cc                   fdsa-cc.mk-123
    2           bb                   32-bb.iuyi-87987asdf(所影响的行数为 3 行)
      

  13.   


    create table tbl1(col1 int, col2 varchar(20) )
    insert into tbl1 
    select 1 ,      'aa'  union all
    select 2 ,      'bb'  union all
    select 3 ,      'cc' 
    create table tbl2(col1 int, col2 varchar(20) )
    insert into tbl2 
    select 1 ,      '12-df.fas-aa.hh'  union all
    select 2 ,      'fdsa-cc.mk-123'  union all
    select 3 ,      '32-bb.iuyi-87987asdf'create table tbl3(col1 int, col2 varchar(20),col3 varchar(30) )insert into tbl3 select a.col1,a.col2,b.col2 from tbl1 a,tbl2 b
    where charindex(a.col2,b.col2)>0select * from tbl3
    col1        col2                 col3
    ----------- -------------------- ------------------------------
    1           aa                   12-df.fas-aa.hh
    3           cc                   fdsa-cc.mk-123
    2           bb                   32-bb.iuyi-87987asdf(3 行受影响)