一个表 t1
字段名   a,b,c表2  t2
字段名   a,b怎样用一条sql实现 把t1中字段a存在与t2中的所有记录的c标记为1,并同步t2的b字段到t1表的b字段呢?例如 
t1 表     a    b    c
          1  
          2
          3t2 表     a    b
          1    1
          2    2我要得到的结果是更新 t1 表为
          a    b    c
          1    1    1
          2    2    1
          3

解决方案 »

  1.   

    update t1
    set b = t2.b,
        c = 1
    from t1,t2
    where t1.a = t2.a
      

  2.   

    update t1 from t2 a set b= t2.b where t1.a=t2.a
    update t1 set c=1 where b is not null
    select * from t1
      

  3.   

    create table t1(a int,b int ,c int)
    insert into t1 values(1,null,null)
    insert into t1 values(2,null,null)
    insert into t1 values(3,null,null)
    create table t2(a int,b int)
    insert into t2 values(1,2)
    insert into t2 values(2,2)
    goupdate t1
    set b = t2.b,
        c = 1
    from t1,t2
    where t1.a = t2.aselect * from t1drop table t1,t2/*
    a           b           c           
    ----------- ----------- ----------- 
    1           2           1
    2           2           1
    3           NULL        NULL(所影响的行数为 3 行)
    */