我有一张几十万条记录的表,其中有company,address,companyid三个字段。
现在想将company,address相同内容记录的companyid更新成一个相同的。
以前companyid都不相同。不想用循环更新,因为那样速度太慢了,
哪位大哥能通过一条语句实现上述功能啊。主要是速度。小弟先谢谢了

解决方案 »

  1.   

    这样??update a
    set companyid=(select min(companyid) from tablename where company=a.company and address=a.address)
    from tablename a
      

  2.   

    不知道這樣可以不
    select A.company,A.address,companyid from table A
    right join
    (select distinct company,address from table) B
    on A.company=B.company and A.address=B.address
      

  3.   

    update 表名 set companyid=c.company
    from
    (
    select * from 表名 a where not exists(select 1 from 表名 where company=a.company and address=a.address)
    ) c
    where company=c.company and address=c.address
      

  4.   

    测试效果如下
    declare @tablename table
    (
    company int,
    address int,
    companyid int
    )
    insert into @tablename
    select 1,2,11 union all
    select 1,2,33 union all
    select 1,3,22 union all
    select 1,4,13 union all
    select 1,2,34update a
    set companyid=(select min(companyid) from @tablename where company=a.company and address=a.address)
    from @tablename aselect * from @tablename/*
    company     address     companyid   
    ----------- ----------- ----------- 
    1           2           11
    1           2           11
    1           3           22
    1           4           13
    1           2           11
    */
      

  5.   

    各位大哥真是好人啊
    我才来csdn,怎么能把分数给你门啊
      

  6.   

    update tbb set companyid=id from (select min(companyid) id,company c,address a from tbb group by company,address) base where a=address and c=company
      

  7.   

    跟BegCsharp写的一样..夜游神的写法也能实现,但性能有差别.
      

  8.   

    测试效果如下
    declare @tablename table
    (
    company int,
    address int,
    companyid int
    )
    insert into @tablename
    select 1,2,11 union all
    select 1,2,33 union all
    select 1,3,22 union all
    select 1,4,13 union all
    select 1,2,34update a
    set companyid=(select min(companyid) from @tablename where company=a.company and address=a.address)
    from @tablename aselect * from @tablename/*
    company     address     companyid   
    ----------- ----------- ----------- 
    1           2           11
    1           2           11
    1           3           22
    1           4           13
    1           2           11
    */LouisXIV(夜游神) 
     他们这样的解决办法真好~!~~让人更快的学到东西!~~谢谢!~~
      

  9.   

    谢谢各位了,我怎么就没想到用min或者max呢
    真笨啊
      

  10.   

    各位能帮我看看我的问题吗?http://community.csdn.net/Expert/topic/4815/4815458.xml?temp=.4763758:)
      

  11.   

    以后多来想各位请教!
    还没做测试,不过俺还是用LouisXIV(夜游神)   的方法