update table1 set a4=b3
from table1 a join table2 b on a1=b1 and a2=b2 and a3=b3
where a3=7

解决方案 »

  1.   

    --楼主自己给的结果有问题吧?对于 table1的第一条记录
    1  2  7   4table2中满足的记录是:
    1  2  7   1替换的结果怎么会是:
    1  2  7   2
      

  2.   

    --写错一个字段,改:update table1 set a4=b4
    from table1 a join table2 b on a1=b1 and a2=b2 and a3=b3
    where a3=7
      

  3.   

    --测试--测试数据
    create table table1(a1 int,a2 int,a3 int,a4 int)
    insert table1
    select 1,2,7,4
    union all select 1,2,4,4
    union all select 4,5,7,2
    union all select 4,5,5,3create table table2(b1 int,b2 int,b3 int,b4 int)
    insert table2
    select 1,2,4,2
    union all select 1,2,5,1
    union all select 1,2,6,6
    union all select 1,2,7,1
    union all select 4,5,4,7
    union all select 4,5,5,1
    union all select 4,5,6,8
    union all select 4,5,7,1
    go--更新处理;
    update table1 set a4=b4
    from table1 a join table2 b on a1=b1 and a2=b2 and a3=b3
    where a3=7
    go--显示更新结果
    select * from table1
    go--删除测试
    drop table table1,table2/*--测试结果
    a1          a2          a3          a4          
    ----------- ----------- ----------- ----------- 
    1           2           7           1
    1           2           4           4
    4           5           7           1
    4           5           5           3(所影响的行数为 4 行)
    --*/
      

  4.   

    update tablle1 set a4=t.b4 
    from 
    (select b4 from table1 a join table2 b on a1=b1 and a2=b2 and a3=b3 where b4=4) t 
    where a4 in
    (select b4 from table1 a join table2 b on a1=b1 and a2=b2 and a3=b3 where b4=7)
      

  5.   

    非常感谢,但我的问题不是这样的
    如果a1=b1 and a2=b2 and a3=b3=7
                            ~~~~~~~
    则将a4的值用a1=b1 and a2=b2 and a3=b3=4,那一行的b4替换
                                    ~~~~~~~
    麻烦再帮忙看看
      

  6.   


    谢谢,vileboy(立晨资讯_追星族_傻子)
    看了一下没看懂
    大楼要关门了
    明天上午试一下就结帐
      

  7.   

    update table1 set a4=c.b4
    from table1 a 
    join table2 b on a.a1=b.b1 and a.a2=b.b2 and a.a3=b.b3 and b.b3=7
    join table2 c on a.a1=c.b1 and a.a2=c.b2 and c.b3=4
      

  8.   

    --测试--测试数据
    create table table1(a1 int,a2 int,a3 int,a4 int)
    insert table1
    select 1,2,7,4
    union all select 1,2,4,4
    union all select 4,5,7,2
    union all select 4,5,5,3create table table2(b1 int,b2 int,b3 int,b4 int)
    insert table2
    select 1,2,4,2
    union all select 1,2,5,1
    union all select 1,2,6,6
    union all select 1,2,7,1
    union all select 4,5,4,7
    union all select 4,5,5,1
    union all select 4,5,6,8
    union all select 4,5,7,1
    go--更新处理;
    update table1 set a4=c.b4
    from table1 a 
    join table2 b on a.a1=b.b1 and a.a2=b.b2 and a.a3=b.b3 and b.b3=7
    join table2 c on a.a1=c.b1 and a.a2=c.b2 and c.b3=4
    go--显示更新结果
    select * from table1
    go--删除测试
    drop table table1,table2/*--测试结果
    a1          a2          a3          a4          
    ----------- ----------- ----------- ----------- 
    1           2           7           2
    1           2           4           4
    4           5           7           7
    4           5           5           3(所影响的行数为 4 行)
    --*/