表的数据如下:
RandomId    ID    NAME    PATH
0          1     名称1    
0          2     名称2
0          3     名称3
0          4     名称4
1          10             .0.1.6.10.
1          11             .0.1.6.11.
1          12             .0.2.6.12.
1          13             .0.2.6.13.
1          14             .0.3.6.14.
1          15             .0.3.6.15.
1          16             .0.3.6.16.
1          17             .0.4.6.17.
1          18             .0.4.6.18.
1          19             .0.4.6.19.
1          20             .0.1.6.20.说明:PATH字段中点与点之间的值为ID字段的值
请问如何用一个UPDATE语句将RandomId为0的记录的NAME字段的值更新到RandomId的值为1的记录的NAME中,条件是RandomId为1的记录的PATH值中存在RandomId为0的记录的ID的值???
也就是如何用一个UPDATE语句得到下面的结果:
RandomId    ID    NAME        PATH
0          1     名称1    
0          2     名称2
0          3     名称3
0          4     名称4
1          10     名称1        .0.1.6.10.
1          11     名称1        .0.1.6.11.
1          12     名称2        .0.2.6.12.
1          13     名称2        .0.2.6.13.
1          14     名称3        .0.3.6.14.
1          15     名称3       .0.3.6.15.
1          16     名称3        .0.3.6.16.
1          17     名称4        .0.4.6.17.
1          18     名称4        .0.4.6.18.
1          19     名称4        .0.4.6.19.
1          20     名称4        .0.1.6.20.

解决方案 »

  1.   

    SQL> create table tmp_table (RandomId varchar2(20),ID varchar2(20),NAME varchar2(20),PATH VARCHAR2(20));Table createdSQL> 
    SQL> insert into tmp_table values(0,1,'名称1','');1 row insertedSQL> insert into tmp_table values(0,2,'名称2','');1 row insertedSQL> insert into tmp_table values(0,3,'名称3','');1 row insertedSQL> insert into tmp_table values(0,4,'名称4','');1 row insertedSQL> insert into tmp_table values(1,10,'','.0.1.6.10.');1 row insertedSQL> insert into tmp_table values(1,11,'','.0.1.6.11.');1 row insertedSQL> insert into tmp_table values(1,12,'','.0.2.6.12.');1 row insertedSQL> insert into tmp_table values(1,13,'','.0.2.6.13.');1 row insertedSQL> insert into tmp_table values(1,14,'','.0.3.6.14.');1 row insertedSQL> insert into tmp_table values(1,15,'','.0.3.6.15.');1 row insertedSQL> insert into tmp_table values(1,16,'','.0.3.6.16.');1 row insertedSQL> insert into tmp_table values(1,17,'','.0.4.6.17.');1 row insertedSQL> insert into tmp_table values(1,18,'','.0.4.6.18.');1 row insertedSQL> insert into tmp_table values(1,19,'','.0.4.6.19.');1 row insertedSQL> insert into tmp_table values(1,20,'','.0.1.6.20.');1 row insertedSQL> commit;Commit completeSQL> select * from tmp_table;RANDOMID             ID                   NAME                 PATH
    -------------------- -------------------- -------------------- --------------------
    0                    1                    名称1                
    0                    2                    名称2                
    0                    3                    名称3                
    0                    4                    名称4                
    1                    10                                        .0.1.6.10.
    1                    11                                        .0.1.6.11.
    1                    12                                        .0.2.6.12.
    1                    13                                        .0.2.6.13.
    1                    14                                        .0.3.6.14.
    1                    15                                        .0.3.6.15.
    1                    16                                        .0.3.6.16.
    1                    17                                        .0.4.6.17.
    1                    18                                        .0.4.6.18.
    1                    19                                        .0.4.6.19.
    1                    20                                        .0.1.6.20.15 rows selectedSQL> 
    SQL> update tmp_table e
      2  set e.name=
      3  (select t.name from tmp_table t
      4  where t.id=substr(e.path,4,1))
      5  where e.randomid=1
      6  ;11 rows updatedSQL> commit;Commit completeSQL> select * from tmp_table;RANDOMID             ID                   NAME                 PATH
    -------------------- -------------------- -------------------- --------------------
    0                    1                    名称1                
    0                    2                    名称2                
    0                    3                    名称3                
    0                    4                    名称4                
    1                    10                   名称1                .0.1.6.10.
    1                    11                   名称1                .0.1.6.11.
    1                    12                   名称2                .0.2.6.12.
    1                    13                   名称2                .0.2.6.13.
    1                    14                   名称3                .0.3.6.14.
    1                    15                   名称3                .0.3.6.15.
    1                    16                   名称3                .0.3.6.16.
    1                    17                   名称4                .0.4.6.17.
    1                    18                   名称4                .0.4.6.18.
    1                    19                   名称4                .0.4.6.19.
    1                    20                   名称1                .0.1.6.20.15 rows selectedSQL>
      

  2.   

    update 表名 a set a.name=(select b.name from 表名 b where b.id=substr(a.path,4,1) and b.randomid=0) where a.randomid=1;