如何将A(xh,cj1)表中的cj1替换为B(xh,cj)中的cj,通过xh关联。

解决方案 »

  1.   

    select a.xh,b.cj as cj1 
    from a,b
    where a.xh=b.xh
    这样么,什么需求?为什么这么做?
      

  2.   

    原来A表中的cj1列的所有值为空,现需替换为B表的cj列的值,需根据xh关联,xh唯一。
      

  3.   

    比如
    A:
    xh   cj1
    001
    002
    003B:
    xh    cj
    001   100
    002   200
    003   300结果:
    A:
    xh    cj1
    001   100
    002   200
    003   300
      

  4.   

    用B表的CJ字段更新A表的CJ1字段的值:
    update a set a.cj1=(select b.cj from b where b.xh=a.xh)
      

  5.   

    至少对于你4楼举的那个例子 5楼给出的update语句是可以满足要求的不过如果在A和B表中存在不对应的数据(比如说在A有xh=004的记录 在B里没有xh=004的记录) 那就难说了 得看你的具体要求是怎样了
      

  6.   

    5楼的SQL需要改进一下 UPDATE a
    SET    cj1 = (SELECT cj FROM b WHERE a.xh = b.xh)
    WHERE  EXISTS (SELECT 1 FROM b WHERE a.xh = b.xh)
      

  7.   


    merge into A
    using B
    on(A.xh=B.xh)
    when matched then            --有则更新
    update set A.cj1=B.cj1;    
    when not matched then        --无则插入
    insert values(B.xh,B.cj1);A(xh,cj1)表中的cj1替换为B(xh,cj)中的cj,通过xh
      

  8.   

    UPDATE a
    SET    cj1 = (SELECT cj FROM b WHERE a.xh = b.xh)
    WHERE  EXISTS (SELECT 1 FROM b WHERE a.xh = b.xh)
      

  9.   

    9和11楼的对
    UPDATE A
    SET    A.cj1 = (SELECT B.cj FROM B WHERE A.xh = B.xh)
    WHERE  EXISTS (SELECT 1 FROM B WHERE A.xh = B.xh)
      

  10.   


    update a set cj1=(select cj from b where a.xh=b.xh)
      

  11.   

    如果 在A,B表中xh都是主键的话可以使用如下语句update (select a.xh,a.cj1,b.cj from A,B where A.xh=b.xh)
       set cj1=cj
      

  12.   

    支持此哥们,Merge into 判断是否合符你的要求,符合则更新,不符合还可以采取insert操作;
      

  13.   


    merge into t_table t
    using(select id,name,age,re,t_id from t_table_1) t1
    on(t.id=t1.id)
    when matched then
     update set t.name=t1.name,t.age=t1.age,t.re=t1.re,t.t_id=t1.t_id(需要更新字段)
    when not matched then insert (t.id,t.name,t.age,t.re,t.t_id) 
    values(t1.id,t1.name,t1.age,t1.re,t1.t_id)
    你可以按以上的格式自己测试一哈...
      

  14.   

    UPDATE A SET cj1 = (SELECT cj FROM B WHERE A.xh = B.xh);
    试试看,估计能解决!如果不能告诉我 下啊