表A 结构如下 撤销部门 cancel_dept cancel_person 撤销人
表B用户表 结构如下 UserName,DeptId
其中 cancel_person 对应UserName 字段唯一.
现表A cancel_dept 这个字段有点问题,部分数据为空.
现在要求 按照表A cancel_person 查询出 DeptId 
然后更新表A的cancel_dept 字段.大概意思就是 update A set cancel_dept=?

解决方案 »

  1.   

    update A set cancel_dept =  (select DeptId from b where UserName = a.cancel_person) where cancel_person in (select distinct DeptId from a)
      

  2.   

    update A set cancel_dept =  (select DeptId from b where UserName = a.cancel_person) where cancel_person in (select distinct DeptId from b)
      

  3.   

    update A set a.cancel_dept=(select deptid from b where a.a.cancel_person=b.UserName)where exists (select 1 from b where a.cancel_person=b.UserName);
      

  4.   

    update A set cancel_dept=(select deptid from b where a.cancel_dept=b.username)
      

  5.   

    update a set cancel_dept =  (select DeptId from b where UserName = a.cancel_person) where cancel_person in (select distinct DeptId from a)
      

  6.   

     有点问题 就是如果 a 表中有数据  b表中没的  就对应不上了 更新为null
    所以 还要再 加上 where exists (select 1 from b where a.cancel_person=b.UserName);
      

  7.   


    where cancel_person in (selectdistinct DeptId from a)
    这个条件不对吧.应该是
    update A set cancel_dept=  (select DeptIdfrom bwhere UserName= a.cancel_person) 
    where A.cancel_dept is null