现在有一张表,字段如下:
id     name    parent_name   parent_id
001    jack      tom
002    tom
数据库现在id、name、parent_name都已经有数据了,但是parent_id还没有,
请问如何用一个update语句,查到parent_id并补上?(就是update上面第一行的parent_id为002).希望各位高手指教!数据库update

解决方案 »

  1.   

    楼主是这个意思吗?
    update table set parent_id=id where  parent_id is null;
      

  2.   

    update table set parent_id=002 where  parent_id is null;
      

  3.   

    是根据parent_name,查到对应的id,再把这id填到parent_id上
      

  4.   


    这个意思吗?
    update table set parent_id=id where parent_name  in(
           select parent_name from table where parent_name ||parent_id is not null
    )
      

  5.   


    这个意思吗?
    update table set parent_id=id where parent_name  in(
           select parent_name from table where parent_name ||parent_id is not null
    )
    不是,就比如jack的父亲是tom,然后现在需要找到tom的id(就是002),填到jack的parent_id上
      

  6.   

    如果parent_name为空的话就不用填parent_id了
      

  7.   


    update t1 
    set parent_id= (select id from  t1 t where t.name = t1.parent_name)
    where parent_id is null and parent_name is not null
      

  8.   

    我本来就是跟8楼一样的写法,但是执行的时候报错,后来发现是因为关联parent_name不唯一的问题,现在已经解决了。不过还是感谢各位的意见!