表 test  id    name   type
-------------------
 2     jj      1
 3     gg      0 
 4     kk      1
 2     uu      0
 3     ww      1
-------------------要求:id相同的两条记录,把type=0的记录的name值更新为type=1的记录的name值。
不知道我描述的清楚不?

解决方案 »

  1.   

    update test a set a.name=(select b.name from test b where a.id=b.id  and b.type=1 and rownum=1) 
     where type=0 and exists (select 1 from test c where a.id=c.id  and c.type=1)
      

  2.   


    我一个小时回来咯!update test a set a.name=(select b.name from test b where a.id=b.id and b.type=1 and rownum=1)  
     where type=0 and exists (select 1 from test c where a.id=c.id and c.type=1);
      

  3.   

    --id相同的两条记录 只是这个条件的话,这个可以
    update test_tt a set a.name=(select b.name from test_tt b where a.id=b.id and b.type=1)
    where exists(select 1 from test_tt c where a.id=c.id and c.type=1) and a.type=0--不是的有多的 则加个伪列update test_tt a set a.name=(select b.name from test_tt b where a.id=b.id and b.type=1 and rownum=1)
    where exists(select 1 from test_tt c where a.id=c.id and c.type=1) and a.type=0
      

  4.   


    --试试这个的更新时间报下
     update (select /*+ BYPASS_UJVC */ a.name name1,b.name name2
     from test_t1 a inner join test_t1 b on a.id=b.id
     where a.type=1 and b.type=0)
     set name2=name1
      

  5.   


    update test t1 set(t1.name)=(select t2.name from test t2 where t1.id=t2.id and t2.type=0) where t1.type=1
    我试了这样子更新 可以 但是貌似只是Oracle适用
      

  6.   

    可以明确告诉你,你这样是欠考虑的! 当你test表中只有type=0,而没有与之id对应的type=1时,更新就会将type=0的name字段更新为null这个问题,我今天犯过错了,不想让你犯错,我们给你的方法才是正确的!
      

  7.   


    --befor--
    /*id   name   type
    2 jj 1
    4 kk 1
    2 uu 0
     3 ww 1
     3 gg 0*/
    --sql
    update test t set name=(select name from test where t.id=id and type=1)--after--
    /*      id     name    type
    2 jj 1
    4 kk 1
    2 jj 0
    3 ww 1
    3 ww 0*/
      

  8.   


    你把你  4 kk  1 这条记录中type=1改成type=0试试你的结果呢!