两张表A,B
A表:
姓名   工号  
张三   001
李四   002
王五   003
张三   004B表:
姓名   保号  工号
张三   B01
李四   B02
王五   B03
李四   B04 现在我要根据 A.姓名=B.姓名 用A表的工号更新B表的工号,只更新A\B中姓名没有重复的记录!
 
结果如下
B表:
姓名   保号  工号
张三   B01   
李四   B02
王五   B03   003
李四   B04 SQL该怎么写????

解决方案 »

  1.   

    update b
      set 工号 = (select 工号 from a where a.姓名 = b.姓名)
      where 姓名 in 
            (select 姓名 from a group by 姓名 having count(*)=1
             minus
             select 姓名 from a group by 姓名 having count(*)=1
             )
      

  2.   

    求差集, A minus B 表示从A中去掉B中有的记录
      

  3.   

    老大,有点不对啊
    我要求用A中的工号替换B中的工号,只对A 和B中姓名没有重复的记录进行替换啊!
    1. 首先要过滤出A 及 B 没有重复的记录
    select 姓名 from a group by 姓名 having count(*)=1
    select 姓名 from b group by 姓名 having count(*)=12.然后才根据过滤出的结果,用A 的工号替换B表我用如下 select 显示结果是正确的:select a.name,b.name from A,a B,b
    where 
    trim(b.name)=trim(a.name)
    and b.name in (select b.name from b group by b.name having count(*)=1)
    and a.name in (select a.name from a group by a.name having count(*)=1)
      

  4.   

    update 要怎么写?
    我写
    update B set b.工号= a.
      

  5.   

    update B set b.工号=
    (select a.工号
    from a, b
    where a.name = b.name
    having count(name) = 1)
      

  6.   

    oracle 中没有 from 子句!
      

  7.   

    我用:
    update B s set gh=(select A.psncode from A p
    where trim(s.xm) = trim(p.psnname)
    and trim(s.xm) in (select s.xm from B s group by s.xm having count(*)=1)
    and trim(p.psnname) in (select p.psnname from A p group by p.psnname having count(*)=1)
    )怎么只更新了几条记录啊?而:
    select  B.xm,A.psnname from B,A 
    where trim(salary_ylbx1.xm) = trim(person.psnname) 
    and salary_ylbx1.xm in (select salary_ylbx1.xm from salary_ylbx1 group by salary_ylbx1.xm having count(*)=1)
    and person.psnname in (select person.psnname from person group by person.psnname having count(*)=1)结果正确!
    why ????????????????????????????????????????????????
      

  8.   

    update B b
       set b.工号 = (select a.工号
                       from A a
                      where a.姓名 = b.姓名
                        and a.姓名 in (select b2.姓名
                                         from B b1, A a1
                                        where b1.姓名 = a1.姓名
                                        group by b2.姓名
                                       having count(1) = 1))
     where exists (select 1
              from A a
             where a.姓名 = b.姓名
               and a.姓名 in (select b2.姓名
                                from B b1, A a1
                               where b1.姓名 = a1.姓名
                               group by b2.姓名
                              having count(1) = 1))
      

  9.   

    解决问题了:---------- 先对两表姓名字段处理:update salary_ylbx1 set xm=trim(xm)....
    --------------只更新姓名没有重复的,两表姓名相同的记录!update salary_ylbx1 s set gh=
    (
    select p.psncode from person p 
    where 
    s.xm =p.psnname
    and s.xm in (select s.xm from salary_ylbx1 s group by s.xm having count(*)=1)
    and p.psnname in (select p.psnname from person p group by p.psnname having count(*)=1)
    )
      

  10.   

    但为什么:
    where trim(s.xm) =trim(p.psnname)
    就不行呢?
    报:单行子查询返回多于一行!
      

  11.   

    trim(s.xm) =trim(p.psnname)
    trim是 怎么用的阿?
    是不是trim有问题啊。
    高手解释一下