有两张表
peoplelist表
id name pid
1  张三  0
2  李四  0
3  王五  0
4  孙大  0
5  赵小  0
......
peoplename表
id name
1  赵小
2  孙大
3  李四
4  张三
5  王五
......
现在想peoplelist的pid对应peoplename的id,
也就是变为
peoplelist表
id name pid
1  张三  4
2  李四  3
3  王五  5
4  孙大  2
5  赵小  1
......数据有近10万条,数据库是sql server,
不知道有什么快捷高效的方法,谢谢

解决方案 »

  1.   

    -- 如果没有过多外键
    select a.id , a.name , b.id as pid into #dk
    from peoplelist as a , peoplename as b where a.name = b.name 
    truncatetable peoplelist 
    go
    insert into peoplelist (id , name , pid) select id , name , pid from #dkdrop table #dk
    go
      

  2.   

    declare @T table(id int,name varchar(10),pid int)
    insert into @t select 1,'张三',0
    union all select 2,'李四',0
    union all select 3,'王五',0
    union all select 4,'孙大',0
    union all select 5,'赵小',0declare @a table(id int,name varchar(10))
    insert into @a select 1,'赵小'
    union all select 2,'孙大'
    union all select 3,'李四'
    union all select 4,'张三'
    union all select 5,'王五'update a set a.pid=b.id from @t a,@a b where a.name=b.nameselect * from @t--这样?
      

  3.   

    update a set a.pid=b.id from peoplelist a inner join peoplename b on a.name=b.name
      

  4.   

    有10万条数据,xeqtr1982(Visual C# .NET)兄的 union all 这样的应该不行啊。还有就是peoplename表有的数据peoplelist表不一定有,
    所以最好update,
    AquaAndVita()兄的写法可能会产生重复数据。
      

  5.   

    update a set a.pid=b.id from peoplelist a left join peoplename b on a.name=b.name
      

  6.   

    union all?那是测试数据阿,用下面那句update不行吗
      

  7.   

    哇,要考虑那么多啊,俺都不晓得这哥们的主键和自增长的情况
    其实,就算有,稍微调整下,就可以啦。外键的话,最好先drop掉,搞完再加回来。
    楼主,就用俺的吧,可快了,我就老这么搞滴,哈哈,不蒙你。
      

  8.   

    --1先对peoplename表建立索引
    create  unique  index u_name on peoplename
    go
    --2更新
    update peplelist set pid=a.id from peoplename a where peoplelist.name=a.name
      

  9.   

    --try
    --1先对peoplename表建立索引
    create  unique  index u_name on peoplename
    go
    --2更新
    update peplelist set pid=peoplename.id from peoplename  where peoplelist.name=peoplename .name