假设A的字段是
id,name,value,age,cidB的字段是
id,name,value,age现在A表中有id=101,cid=5的一条记录,但这条记录的name,value,age都是空的。
而B表中有id=101的记录,这条记录的name,value,age都是有值的。请问可否用SQL语句来实现将B表中id=101记录中的name,value,age三个值传到A表中的id=101的这条记录上?

解决方案 »

  1.   

    update A
    set A.name=(select name from B where B.id=A.id),
    A.value=(select value from B where B.id=A.id),
    A.age=(select age from B where B.id=A.id)
    ;我也是初学者,看到你的问题,我试了下,以上可以实现你需要的需求,不过有些繁琐,呵呵。你再看看修改得简单点吧。觉得可以的话,还请采纳我哦,顺便把你修改过的回复给我哦,O(∩_∩)O~互相学习嘛
      

  2.   


    merge into A 
    using (select id,name,value,age from B) b
    on a.id=b.id
    when matched then 
    update set a.name=b.name,a.value=b.value,a.age=b.age
      

  3.   


    merge into A 
    using (select id,name,value,age from B) b
    on a.id=b.id
    when matched then 
    update set a.name=b.name,a.value=b.value,a.age=b.age
      

  4.   


    方法一:使用merge into,效率高
    merge into a
    using b on 
    (a.id=b.id)
    when matched then
         update
         set a.name=b.name,
             a.value=b.value,
             a.age=b.age;  
    --
    方法二:更新b表
    update a
    set (a.name,a.value,a.age)=
        (select b.name,b.value,b.age
        from a,b
        where a.id=b.id
          and a.name is null
          and a.value is null
          and a.age is null
        );
      

  5.   


    update a
    set (a.name,a.value,a.age)=
        (select b.name,b.value,b.age
        from a,b
        where a.id=b.id)
    where exists(
          select 1
          from a,b
          where a.id=b.id
            and a.name is null
            and a.value is null
            and a.age is null
          );
      

  6.   

    update a set (a,b,c)= (select a,b,c from b where b.id=?) where a.id=?